angular.js: Send some data to php-script

前端 未结 1 1281
抹茶落季
抹茶落季 2021-01-25 05:23

This is my template of an angular.js SPA:

1条回答
  •  悲&欢浪女
    2021-01-25 05:39

    First off... USE ng-model, it will make your life so much easier

    Second, within your controller, you are going to need to use $http.post to send the data to your php endpoint/script.

    app.controller("SomeController", function($scope, $http){
     $scope.sendFunction = function (array) {
     var data = {
        array: array // The array of data you are passing in
     }
      $http.post('yourPhpEndpoint.php', data).success(function(data){
     //This means that you have succeeded 
       }).error(function(){
      //This means that it failed
       })
     }
    })
    

    Third, I'm going to assume that you are using a normalized database and would like to enter these arrays into a table one by one... In Your PHP File.

     $data = json_decode(file_get_contents('php://input'));
     $submittedInfo = $data->array;
    
     foreach($submittedInfo as $arrayItem){
        // Do an insert into the database
     }
    

    0 讨论(0)
提交回复
热议问题