0

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.

6
  • what error you get after execution Commented Sep 29, 2015 at 8:37
  • @ddw147 : I am not getting any error but values are adding in the last row of the table always after each submit.But here i need to add in 1st row of the table always after each submit. Commented Sep 29, 2015 at 8:39
  • checkout stackoverflow.com/questions/17826766/… Commented Sep 29, 2015 at 8:41
  • @ddw147 : No,I used that method but the output is not coming. Commented Sep 29, 2015 at 8:52
  • 1
    replacing .push with .unshift will do Commented Sep 29, 2015 at 9:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.