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.

4
  • your question is too broad. this may help: stackoverflow.com/a/19986623/2460773 Commented May 13, 2015 at 7:27
  • 1
    You should take a look at ng-model. 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 Commented May 13, 2015 at 7:29
  • @Okazari: And than I send the array via http post to the php/db? Commented May 13, 2015 at 9:13
  • Yep, you can use the $http service from angular to post your request to your php endpoint. Commented May 13, 2015 at 9:31

1 Answer 1

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
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Is it possible to "serialize" the input-fields to get the array or do I have to build the array manually?

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.