-1

I have to submit array of values using jquery and angularjs. When i click submit button i'm getting only first array value. How to get all array value using ng-model.Here is my all code https://jsfiddle.net/rijo/6dLxhr7j/ Script code given below

$scope.onSubmit = function(){
    sum = $scope.obj;
    console.log(sum);
        $http({
            method : 'POST',
            url : 'admin_controller.php',
            data: $.param(sum),
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).then(function (response) {
            console.log("OK");
        });
    }

Please any one can help.. Thanks!

Rijo
  • 2,963
  • 5
  • 35
  • 62
  • The AngularJS framework normally posts data as `'Content-Type: application/json` and there is no need to serialize it with the jQuery param method. – georgeawg Mar 04 '17 at 22:06
  • Look at [StackOverflow: Receive JSON POST with PHP](http://stackoverflow.com/questions/18866571/receive-json-post-with-php). – georgeawg Mar 04 '17 at 22:14

1 Answers1

-1

You have to post data as an object, pass your array as an object attribute value. In this case I'm using the $http.post shortcut method.

function buildParam(){
    // write any needed logic to get, build your array
    // maybe a loop extracting values from collection, etc.
    var myArray = []; // here your array of values, objects, etc.
    return myArray;
}

$scope.onSubmit = function(){
    sum = {
        myAttr: buildParam()
    };

    $http.post('admin_controller.php', sum).then(
        function(response) {
            console.log('ok');
        }       
    );
}
aUXcoder
  • 1,048
  • 1
  • 20
  • 32
  • I want to get the value as dynamic, not a static – Rijo Mar 04 '17 at 15:10
  • can u give me how to get dynamic values from my html code – Rijo Mar 04 '17 at 15:12
  • The $http service automatically transforms request data with `angular.toJson`. See [AngularJS $http Service API Reference - Default Transformations](https://docs.angularjs.org/api/ng/service/$http#default-transformations) or see it for yourself in the [$http service source code line #293](https://github.com/angular/angular.js/blob/master/src/ng/http.js#L293). – georgeawg Mar 04 '17 at 21:59