Send JavaScript array via Ajax with jQuery to PHP file

0

I want to send an array via jQuery using Ajax to another PHP file, however I do not know how to access it in PHP. I'm sending by GET I do not know if it would be ideal either.

nome_receita is a variable and ingredientes is the array that I need to "open" in PHP.

$.get("http://localhost/estudos/oquetemprahj/servidor.php?nome_receita="+nome_receita+"&&ingredientes="+ingredientes+"",function(retorno)
{
	alert(retorno);
});
    
asked by anonymous 29.07.2016 / 18:19

4 answers

1

Just add [] after the variable name. Example:

$.get("http://localhost/estudos/oquetemprahj/servidor.php?nome_receita="+nome_receita+"&ingredientes[]=item1&ingredientes[]=item2&ingredientes[]=item3",function(retorno)
{
    alert(retorno);
});

Simply replace item with the value of the key. In PHP it will arrive as Array, you can do the test:

print_r($_REQUEST["ingredientes"]);

If it's a checkbox, for example, you can do the following:

<input type="checkbox" value="ingrediente_x" name="ingredientes[]">
<input type="checkbox" value="ingrediente_y" name="ingredientes[]">
<input type="checkbox" value="ingrediente_z" name="ingredientes[]">
    
07.01.2017 / 13:28
0

I believe that for this type of scenario a POST is more appropriate, in this case try to do the following:

var data = {
    nome_receita: nome_receita, 
    ingredientes: ingredientes
};

var httpRequest = new XMLHttpRequest();
httpRequest.open("POST", "http://localhost/estudos/oquetemprahj/servidor.php", true);
httpRequest.responseType = "json";
httpRequest.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
httpRequest.send(JSON.stringify(data));

If you want to test sending by GET , simply replace POST with GET on open(...) .

With jQuery you can try the following:

var data = {
    nome_receita: nome_receita, 
    ingredientes: ingredientes
};

$.get("http://localhost/estudos/oquetemprahj/servidor.php", data, function(retorno)
{
    alert(retorno);
});
    
29.07.2016 / 18:26
0

In the PHP script that will receive the data, you simply access using $ _GET, or $ _POST, if the method is post. Then it would look something like this:

<?php
    $nome_receita = $_GET['nome_receita'];
    $ingredientes = json_decode($_GET['ingredientes']);
?>

I believe that as ingredients it is an array, you have to use the PHP function json_decode ().

It's also important to note that you used & twice for the ingredients variable in the URL passage.

    
29.07.2016 / 18:53
0

I think you can use the jQuery $ .ajax function, but first you need to transform your array into a JSON string. One good way is to use JSON.stringify. Link to the JSON.stringify () documentation

Code to make the ajax call:

$.ajax({
  url: 'http://localhost/estudos/oquetemprahj/servidor.php',
  type: 'GET', // Tipo de requisição, podendo alterar para GET, POST, PUT , DELETE e outros metodos http
  data: {nomeReceita: nome_receita, ingredientes: ingredientes},
  success: function(resposta){
     //Bloco com a resposta do servidor caso a requisição ocorra normalmente
  },
  error: function(resposta){
    //Entrará aqui caso não de certo a requisição 
  }
})

Now in your php you will have to get this data and pass and pass them from JSON string to object.

<?php

$nome_receita = $_GET['nome_receita'];
$ingredientes = json_decode($_GET['ingredientes']);
    
29.07.2016 / 19:48