Buck Buck - 2 months ago 6
PHP Question

Serialize an ajax array

I have an array make from some input value get with jQuery like this

var ingredient = [];
text.each(function(){
ingredient.push($(this).val());
});


and send with ajax like this

$.ajax({
url: "update_recipe.php",
type: "post",
data: ingredient,
dataType: "html",
success : function(code_html, statut){
console.log(code_html);
console.log(statut);
},
error : function(resultat, statut, erreur){
console.log("La requĂȘte n'a pas aboutie...");
console.log(resultat);
console.log(statut);
console.log(erreur);
}
});


but when i need to get the array with php, $_POST is NULL...

Do you have an idea of how can i send this array ?

thanks

Answer

You can create a query string having "key=value" pairs where "key" is the name attribute value of the element, instead of pushing only value to an array.

var ingredient = "";

text.each(function(i) {
    ingredient += this.name + "=" + this.value + (i < text.length ? "&" : "");
});
Comments