Sending an Array by POST with JSON for PHP

1

Next, I need to send an array by POST with JSON. I have read about JSON.stringify and in my PHP code I use json_decode, but in practice I am not able to apply. I'll put the code here and change it more or less with the idea that I hope, even wrong, but only to make it easier to understand the logic.

HTML / Script

$("#ok").click(function() {
const nomes = ValorTextArea.value

var ArrayNomesComQuebraDeLinha = nomes.split('\n')   

    $.ajax({
        type: 'post',
        dataType: 'json',
        url: 'busca.php',
        data: ***ArrayNomesComQuebraDeLinha***
        async: true,
            success: function(dados){
                //Retorno da pagina busca.php, como os dados vao vir em ordem a ideia é colocar em uma table, se retornar o valor fica do lado, se nao fica vazio.
                nome   |idade
                junior |23
                joao   |N/A
            }

    });
  });  

PHP / SQL

include "conexao.php";

$nomes = $_POST['ArrayNomesComQuebraDeLinha']

for(var i=0; i< nomes.lenght;i++){
$select = "SELECT IDADE FROM clientes WHERE NOME" +$nomes[i];
$result = mysqli_query($conexao, $select);
while ($row = mysqli_fetch_assoc($result)) { 
$idade[] = array_map('utf8_decode', $row);
}
echo json_encode($idade);
}

I apologize if I could not be clear with the explanation or using the logic I expect along with my code. This is my first post.

Thanks in advance.

    
asked by anonymous 13.08.2017 / 23:49

1 answer

0

First of all your javascript and php code contains errors , and possibly that is why it is not working.

  

The date option of the jquery ajax function, gets parameters between    keys and not by asterisk

Change the line:

data: ***ArrayNomesComQuebraDeLinha***

By:

data: { ArrayNomesComQuebraDeLinha },
  

PHP in the $ names row that receives the data   sent via POST is missing semicolon change to:

$nomes = $_POST['ArrayNomesComQuebraDeLinha'];
  

Missing sign of equal (=) also indicates that the NAME column should be = > the variable $ names [i]

     

PHP to concatenate string does not use the + sign , p>

Looking like this:

$select = "SELECT IDADE FROM clientes WHERE NOME = '" . $nomes[i] . "'";
    
14.08.2017 / 00:41