How to pass multiple arrays to PHP page

Asked

Viewed 159 times

1

$scope.items = [];

dados = $('#meu_form').serialize();

I wonder: how to send/receive/read the arrays above to a PHP page?

Javascript:

    app = angular.module("app",[]);
    app.controller("controlador", ["$scope", "$http",

    function($scope, $http){
    $scope.items = [];

    $scope.submitForm = function() {
        var dados = $('#meu_form').serialize();

        $http({
          method  : 'POST',
          url     : 'pagina.php',
          data    :
          headers : {'Content-Type': 'application/x-www-form-urlencoded'} 
         })

    };

php page.

$dados = json_decode(file_get_contents('php://input'), true);

foreach ($dados as $key => &$value) {
    $codigo     = $value['codigo'];
    $quantidade = (float)$value['quantidade'];
    $v_total    = $value['v_total'];

}

$nome  = $_POST['nome'];
$email = $_POST['email'];
  • Within $http in data : try to put data : {scope: $scope.items, dados: dados }, and in PHP $_POST['scope'] and $_POST['dados'] to recover respectively.

  • Thanks for the @ivcs attention, but it didn’t work. I can invite you to chat?

  • Strange, here worked, returns some error in the console? I tried with the arrays: var lista1 = ['item1', 'item2', 'item3']; var lista2 = ['item1', 'item2', 'item3', 'item4']; and the date with { lista1: lista1, lista2: lista2 }, in PHP I only walked the $_POST and concatenated the keys into a string, the result was expected.

  • -- Can yes :)))

  • Just a minute, @ivcs.

  • http://chat.stackexchange.com/rooms/51217/como-passar-multiplos-arrays-para-pagina-em-php

Show 1 more comment

1 answer

2


In the JS, you just ride one another array with these arrays, in the field of data of $http, thus:

app = angular.module("app",[]);
app.controller("controlador", ["$scope", "$http",

function($scope, $http){
$scope.items = [];

$scope.submitForm = function() {
    var dados = $('#meu_form').serialize();

    $http({
      method  : 'POST',
      url     : 'pagina.php',
      data    : {scope: $scope.items, dados: dados}
      headers : {'Content-Type': 'application/x-www-form-urlencoded'} 
     })

};

And in the PHP, you can take the values like this:

<?php 

// transforma o JSON enviado em array associativo
$dados = json_decode(file_get_contents('php://input'), true); 

$p = $dados['dados']; // pega o formulário serializado do AJAX 
parse_str($p, $dados['dados']); // tranforma em array de PHP 

?>

Thus, it follows the legend of identification of the variables:

  • $dados['scope'] - would equal the $scope.items.
  • $dados['dados'] - would equal the $('#meu_form').serialize(), already in array.

To access a form field, such as input email, you will use:

$dados['dados']['email']

Already to access a field of email of an element in the Scope, will be:

$dados['scope'][0]['email']

If you want to go through all the elements of your Scope, will be:

foreach($dados['scope'] as $item) {
    echo '<br>Nome: '  .  $item['nome'];
    echo '<br>Email: ' .  $item['email'];
}

Soon, $item['email'] would be the same $dados['scope'][X]['email'], where X is the element currently covered by foreach.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.