How to send Jquery Json array to PHP

Asked

Viewed 1,797 times

1

I have a table where the lines constantly change order, I need to record this table in the bank. Since I will no longer modify the table I click on a "Confirm Grid" button, at that moment save this table in an array within jquery, and when I want to send php click to submit in the form.

However I cannot receive this data on 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?

  • What you’re trying to do doesn’t exist, you can’t send an Alert echo through PHP, when you use Smarty, the output is sent through a compiled array that generates a visualization template of this compilation, what will be seen is sent in a view via an element calling $smarty->assign('variavel', $dados). Note that by the layout of your view this is Smarty.

  • Here is the Smarty documentation

  • I just used this echo to test if I was getting information from the front. It really doesn’t interest me.

  • You need to put the correct code then PHP, so that someone can help you in this. Because it seems to me very incomplete your question.

3 answers

2

To receive the information in the post as "pilots" it is necessary to pass the index and value in the date of ajax, it is important in the back-end to use the "isset($_POST['pilots'])", to check if there really is an index with value being received, and treat the possible Warning.

If the return of the back is not in JSON format it is necessary to change the "dataType", in accordance with 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);
        }
    });

});
  • When checking isset($_POST['pilots']), it does not enter if. What would be the "Valuereturn"?

  • The fact that he does not access the isset is exactly pq the Dice ['pilots'] is coming with empty value, to debug this put before the if : echo "<pre>"; die(var_dump($_POST));

0

0

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

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

One can also use the constant STDIN, which amounts to php://input:

$conteudo = stream_get_contents(STDIN);

Then just turn this content into an array:

$conteudo = json_decode($conteudo, true);

Browser other questions tagged

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