C
C
Cheizer2018-02-19 13:21:10
JavaScript
Cheizer, 2018-02-19 13:21:10

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">


Next, the form is sent to form.js
There are a lot of field checks, error animations, etc., so SERIALIZE will not work, since everything is tied to the LABEL tag
example
<label class="name">
<input type="text" value="" name="name" placeholder="Введите ваше имя:">
<span class="error error-empty">*</span>
</label>


The rest of the fields are the same, it's me that serialize is not suitable, then everything flies to js

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); 
},


Nothing fucking arrives in php, or I messed up again and here
$options = "";
if (isset($_POST["options"])) {
$options = $_POST["options"]; 
}

foreach ($options as $value){
$result .= '
<tr>
<td>
  '.$value[name].' 								
</td>
<td>
'.$value[val].' 
</td>
</tr>
';}

Next, I display $result in the right place.

So this is where I messed up? Why is nothing coming from php? Or I don’t output it in php like that, BUT if serialize is used, then everything in php works and arrives, but serialize doesn’t fit. PAMAGITIA!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander null, 2018-02-19
@Cheizer

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 question

Ask a Question

731 491 924 answers to any question