2016-09-17 4 views
1

У меня есть массив сделать из некоторых входного значения получить с JQuery, как этотсериализовать Аякс массив

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

и отправить с помощью AJAX, как этого

$.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); 
    } 
    }); 

но когда мне нужно получить массив php, $ _POST - NULL ...
У вас есть идея, как я могу отправить этот массив?

благодаря

+0

Показать код PHP, проверить значение '' ingredient' с console.log' –

+0

console.log массива, как [ "2 oeufs", "125 г де Farine", "25 мл де-лайт" , «1/2 cuillère à café de sure», «1 cc de levure chimique», «30 g de beurre fondu», «1/2 cc de sel», «1 cs d'eau de fleur d», oranger "] И php - это просто var_dump ($ _ POST) – Buck

ответ

1

Вы можете создать строку запроса, имеющего "ключ = значение" пар где «ключ» - значение атрибута name элемента, вместо того, чтобы нажимать только значение на массив.

var ingredient = ""; 

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

Кажется, строка не получает мой php-скрипт. var_dump ($ _ POST); display: array (0) {} – Buck

+0

@Buck У входных элементов есть атрибут 'name'? – guest271314

+0

Теперь, да ... правильное решение - это имя = "ингредиент []" спасибо :) – Buck

0

// с помощью AJAX

// JS

var data = $("#form :input").serializeArray(); 
data = JSON.stringify(data); 
post_var = {'action': 'process', 'data': data }; 
$.ajax({.....etc 

// PHP

$data = json_decode(stripslashes($_POST['data']),true); 
print_r($data); // this will print out the post data as an associative array 
+0

не нужно посылать как json, jQuery будет кодировать этот массив внутри – charlietfl

+0

Я могу просто декодировать этот массив с помощью PHP? – Buck

+0

да, вы можете это сделать – siddhesh

Смежные вопросы