Skip to content Skip to sidebar Skip to footer

How To Post Array Multidimensional Angular Js

I have an array in angularjs, Example as below. $scope.order.qty='20'; $scope.order.adress='Bekasi'; $scope.order.city='Bekasi'; This array can post with this code $http({

Solution 1:

You can pass array like it:

$http({
      method  : 'POST',
      data    : { items: $scope.items }
      ...
     })

getting data:

$_POST = json_decode(file_get_contents('php://input'), true);
$items = $_POST['items'];

Solution 2:

Your json will look like this if you send $scope.items :

[
  {
    "kode_produk": "PR_1"
  },
  {
    "kode_produk": "PR_2"
  },
  {
    "kode_produk": "PR_3"
  }
]

Which results to this php array after $input = json_decode(...):

array (size=3)
  0 => 
    object(stdClass)[1]
      public 'kode_produk' => string 'PR_1' (length=4)
  1 => 
    object(stdClass)[2]
      public 'kode_produk' => string 'PR_2' (length=4)
  2 => 
    object(stdClass)[3]
      public 'kode_produk' => string 'PR_3' (length=4)

You have an array of objects, not a multidimensional-array!

You can iterate over the items like:

foreach($input as $item)
{
    echo $item->kode_produk;
}

Solution 3:

Is one way

in JavaScript send data $scope.items, for example:

$http({
          method  : 'POST',
          url     : '<?php echo base_url(); ?>add_order',
         data    : $scope.items,
          headers : {'Content-Type': 'application/x-www-form-urlencoded'} 
         })

and on the site PHP code write:

$_POST = json_decode(file_get_contents('php://input'), true);
var_dump($_POST); die();

and analyze the data structure.


Post a Comment for "How To Post Array Multidimensional Angular Js"