How to send Json array from Jquery to PHP

1

I have a table where the rows constantly change order, I need to write this table to the bank. From the moment I will no longer modify the table I click on a "Confirm Grid" button, at that time saved that table in an array inside the jquery, and when I want to send pro php clico to submit in the form.

But I can not get this data in the back, I get a "Notice: Undefined index: pilots"

html:

    <table id="tabelaPilotos" class="table table-striped">
      <tbody>
      {foreach from=$pilotos item=row}                        
           <tr>            
             <td>
               {$row.numero}
             </td>
             <td>
             {$row.nome}
            </td>   
            </tr>
    {/foreach}
       </tbody>
    </table>
    <input type="button" id="add" value="Confirmar Grid" >
    <form name="form_insert" method="post" id="form_insert">
       <fieldset style="display: none;"></fieldset>
       <label>
           <input type="submit" id="confirmar" name="confirmar" value="Cadastrar grid" />
    </label>
    </form>

jQuery:

    var pilotos = [];

$("#add").click(function () {
    $('#tabelaPilotos tbody tr').each(function () {
        var colunas = $(this).children();
        var piloto = {
            'numero': $(colunas[0]).text(), 
            'nome': $(colunas[1]).text()
        };
        pilotos.push(piloto);
    });
    console.info(pilotos[0]);
});

$("#form_insert").submit(function () {    
    $.ajax({
        type: 'POST',
        cache: false,
        data: pilotos,
        dataType: "json",
        url: 'cad_corrida.php',
        success: function (msg) {
            alert(msg);
        }
    });

});

php:

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
       $pilotosarray = $_POST["pilotos"];
       echo "<script>alert('$pilotosarray')</script>";
     }

How to recover this array?

    
asked by anonymous 01.07.2016 / 14:29

3 answers

2

To receive the information in the post as "pilots" you need to pass the index and value on the ajax date, it is important in the backend to use the "isset ($ _ POST ['pilots' strong>, to check if there is really an index with value being received, and to treat the possible warning.

If the return of the back is not in JSON format, it is necessary to change the dataType , according to the expected return.

$("#form_insert").submit(function () {    
    $.ajax({
        type: 'POST',
        cache: false,
        **data: { pilotos: pilotos}**,
        **dataType: "ValorRetorno"**,
        url: 'cad_corrida.php',
        success: function (msg) {
            alert(msg);
        }
    });

});
    
01.07.2016 / 14:56
0

The content that is sent to the server goes to the body of the request. That is, you can read it like this:

$conteudo = file_get_contents('php://input');

You can also use the constant STDIN , which is equivalent to php://input :

$conteudo = stream_get_contents(STDIN);

Then just turn that content into an array:

$conteudo = json_decode($conteudo, true);
    
01.07.2016 / 14:38
0

Gabriel,

You can use JSON.stringify in javascript to be able to transform your object into a string and pass that string to your ajax "date" and in PHP you use the json_decode function to turn this string back into an array / object .

I did not test with code, but theoretically this idea works.

Reference:

  

JSON.stringify:    link

     

json_decode: link

I hope it helps.

Vlw

    
01.07.2016 / 18:16