Answer the question
In order to leave comments, you need to log in
How to AJAX pass a multidimensional array to PHP?
The cap is already tearing, I’ve been struggling for the second day and can’t solve the problem, coding gurus, please see what’s wrong, why does the array print out in the console, and emptiness arrives in php?
There is a form on the site, with the usual fields like email, phone, etc., and there are custom SELECTs, created automatically, the number is unknown, the result of each select selection is saved in two hidden fields, 1 is the name of the select, 2 is the choice of option. As a result, we have (for example, the result of 2 selects):
1 результат работы селекта
<input type="hidden" class="option" name="option[1][name]" value="РАЗМЕР">
<input type="hidden" class="option" name="option[1][val]" value="XL">
2 результат работы селекта
<input type="hidden" class="option" name="option[2][name]" value="ШТ">
<input type="hidden" class="option" name="option[2][val]" value="5">
<label class="name">
<input type="text" value="" name="name" placeholder="Введите ваше имя:">
<span class="error error-empty">*</span>
</label>
submitFu: function () {
$.ajax({
type: "POST",
url: _.mailHandlerURL,
data: {
name: _.getValFromLabel($('.name', _.form)),
options: _.useroption // вот тут наш массив
},
})
},
useroption: function(){
var options = [];
// передавать в пост можно только одномерный массив,
// поэтому извращаемся с вот таким описанием данных'
$('input.option', _.form).each(function (i) {
options['option[' + i + '][name]'] = $(this).val();
options['option[' + i + '][val]'] = $(this).val();
});
console.log(options);
},
$options = "";
if (isset($_POST["options"])) {
$options = $_POST["options"];
}
foreach ($options as $value){
$result .= '
<tr>
<td>
'.$value[name].'
</td>
<td>
'.$value[val].'
</td>
</tr>
';}
Answer the question
In order to leave comments, you need to log in
1. Why won't serialize work? if everything is in the data in the input, even if the input is wrapped in a lable, this does not mean that there is no data in the input
2. where does the request go? if to another domain, for example, then you need to check if the permissions are for cross-domain requests
3. check what comes without ajax, just submit the form, and see what is in $_REQUEST
4. you can make a screen with console.log(form.serialize()); naturally where form is your form
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question