简体   繁体   中英

AngularJS send updated data to php page via $http.post

I have just added the functionality to edit a table cell in my angularJS app. What I would like to do now is have the changes reflected in the database by sending the updated data to my PHP script, I'm a little stuck on how to actually resend the updated table.

My Angular Table in question:

<tr ng-repeat="c in resultValue=(data | filter:{'type':typeFilter} | filter:dateFilter | filter:proFilter | filter:cusFilter | filter:taskFilter | filter:nameFilter)">

    <td class="jid" ng-hide="viewField">{{c.journal_id}}</td>
    <td ng-show="modifyField"><input type="text" class="in1" ng-model="c.journal_id" /></td>

    <td class="wda" ng-hide="viewField">{{c.work_date}}</td>
    <td ng-show="modifyField"><input type="text" class="in1" ng-model="c.work_date" /></td> 

</tr>
     <button ng-hide="viewField" ng-click="modify(c)">Modify</button>
     <button ng-show="modifyField" ng-click="update(c)">Update</button>

The controller thanks to a SO answer for the edit part:

    journal.controller('dbCtrl', ['$scope', '$http', function ($scope, $http) {

          $scope.loadData = function () {
        $http.get("http://localhost/slick/journalFetch.php").success(function(data){
                $scope.data = data;
            }).error(function() {
                $scope.data = "error in fetching data";
            });
}

  $scope.modify = function(c){

            $scope.modifyField = true;
            $scope.viewField = true;
        };


  $scope.update = function(c){
            $scope.modifyField = false;
            $scope.viewField = false;

          //AM I ABLE TO RESEND THE UPDATED (c) DATA HERE TO THE DATABASE ?

             $http({
                method: "post",
                url: "update.php",
                data: {
                    //if so how to retrieve updated c data here?
                }
             }); 
            };
  $scope.loadData();
}]);

Looks like you are trying to update the entire table with a single Update button click. Your current code is trying to access c outside the tr element which makes c out of scope for the button .

Try passing data variable to the update function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM