I need to bind data in the first row of the table always after submitting the data into database using Angular.js and PHP. Here is my code:
courseController.js:
var courseApp=angular.module('GofastoHome');
courseApp.controller('coursecontroller',function($scope){
$scope.buttonName="Submit";
//console.log($scope.buttonName);
var id=gup( "e_i" );
$.ajax({
type:'GET',
url:"php/readCourseData.php",
success: function(data){
$scope.$apply(function(){
$scope.courseData=angular.fromJson(data);
});
}
})
$scope.addCourseData=function(){
if($scope.coursename==null){
alert('course name field could not blank');
}else if($scope.course_short_name==null){
alert('short name field could not blank');
}else if($scope.semester==null){
alert('semester field could not blank');
}else{
var userdata={'course_name':$scope.coursename,'course_short_name':$scope.course_short_name,'semester':$scope.semester};
console.log('userdata',userdata);
$.ajax({
type:'POST',
url:"php/addCourse.php",
data:userdata,
dataType: 'json',
success: function(response){
$scope.$apply(function(){
console.log('response',response);
alert(response.msg);
$scope.coursename=null;
$scope.course_short_name=null;
$scope.semester=null;
$scope.courseData.push(response);
});
},
error: function(result){
alert(result)
}
})
}
}
if(id != ''){
$scope.buttonName="Update";
var userdata={'userid':id};
$.ajax({
type:'GET',
url:"php/editCourseData.php",
data:userdata,
success: function(data){
$scope.$apply(function(){
var result=JSON.parse(data);
$scope.coursename=result[0].course_name;
$scope.course_short_name=result[0].short_name;
$scope.semester=result[0].semester;
});
},
error: function(data){
}
})
}
});
Inside the success function the line $scope.courseData.push(response) is responsible to bind data into the table. Here the values are added in the last row of the table.
addCourse.php:
<?php
$course_name=stripslashes($_POST['course_name']);
$course_short_name=stripslashes($_POST['course_short_name']);
$semester=stripslashes($_POST['semester']);
$con = mysql_connect('localhost', 'root', '******');
mysql_select_db('go_fasto', $con);
$qry ='INSERT INTO db_course (course_name,short_name,semester) values ("' . $course_name . '","' . $course_short_name . '","' . $semester . '")';
$qry_res = mysql_query($qry);
if ($qry_res) {
$result['msg'] = "Course has added successfully";
} else {
$result['msg'] = "course could not added ";
}
$query='SELECT * from db_course order by course_id desc';
$res=mysql_query($query);
$result=mysql_fetch_array($res);
echo json_encode($result);
?>
course.html:
<table class="table table-bordered table-striped table-hover" id="dataTable">
<colgroup>
<col class="col-md-1 col-sm-1">
<col class="col-md-2 col-sm-2">
<col class="col-md-2 col-sm-2">
<col class="col-md-2 col-sm-2">
<col class="col-md-2 col-sm-2">
<col class="col-md-2 col-sm-2">
<col class="col-md-1 col-sm-1">
</colgroup>
<thead>
<tr>
<th>Sl. No</th>
<th>Cource Name</th>
<th>Short Name</th>
<th>No of Semester</th>
<th>Edit</th>
</tr>
</thead>
<tbody id="detailsstockid">
<tr ng-repeat="course in courseData">
<td>{{$index+1 }}</td>
<td>{{course.course_name}}</td>
<td>{{course.short_name}}</td>
<td >{{course.semester}}</td>
<td>
<a href='#course?e_i={{course.course_id}}' >
<input type='button' class='btn btn-xs btn-green' value='Edit' >
</a>
</td>
</tr>
</tbody>
</table>
Here my requirement is when user will submit the data the same data will add inside the table in first row always.