1

This is my template of an angular.js SPA:

<div data-id="{{id}}" ng-repeat="id in array.id">
    <input type="text" name="value1" value="{{value1}}">
    <input type="text" name="value2" value="{{value2}}">
</div>

The data for this template is loaded via http post request in a controller.

Now I want to send this data back to the php-script to save it in the DB. I would like to do that in a directive, but I don't know how to send the data dynamically - as there are multiple rows and some templates differ a little bit.

user3142695
  • 15,844
  • 47
  • 176
  • 332
  • your question is too broad. this may help: http://stackoverflow.com/a/19986623/2460773 – Nitsan Baleli May 13 '15 at 07:27
  • 1
    You should take a look at ng-model. https://docs.angularjs.org/api/ng/directive/ngModel. You could use ng-model like this on your inputs "ng-model="array.id.value1"" then you'll be able to find your value in your array.id scoped variable – Okazari May 13 '15 at 07:29
  • @Okazari: And than I send the array via http post to the php/db? – user3142695 May 13 '15 at 09:13
  • Yep, you can use the $http service from angular to post your request to your php endpoint. – Okazari May 13 '15 at 09:31

1 Answers1

0

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

<div data-id="{{id}}" ng-repeat="id in array.id">
<input type="text" name="value_{{$index}}" ng-model="id.value">
</div>

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
 }
Robo Rick
  • 721
  • 6
  • 7