C
C
Cheizer2018-02-18 21:43:37
JavaScript
Cheizer, 2018-02-18 21:43:37

How to pass array from form to PHP using AJAX method?

Friends, there are no direct thoughts, I don’t know how to do it, for the first time I encounter such a task.

There is a form with custom SELECTs on the site, which are created automatically, their number is unknown.
Each SELECT writes the result of the selection, the name and value of the parameter into its hidden fields:

<div class="option">
<span class="name">РАЗМЕР</span>
<select class="option-select">
......
</select>
<input type="hidden" class="name-option" name="name-option[]" value="РАЗМЕР">
<input type="hidden" class="value-option" name="value-option[]" value="XL">
</div>

<div class="option">
<span class="name">ШТ</span>
<select class="option-select">
......
</select>
<input type="hidden" class="name-option" name="name-option[]" value="ШТ">
<input type="hidden" class="value-option" name="value-option[]" value="5">
</div>

<div class="option">
<span class="name">ПОЛ</span>
<select class="option-select">
......
</select>
<input type="hidden" class="name-option" name="name-option[]" value="ПОЛ">
<input type="hidden" class="value-option" name="value-option[]" value="М">
</div>

.....


Next, the form is sent to the form.js file, from where AJAX passes it to the php file for submission.
A form.js file accept like this

$.ajax({
    type: "POST",
    url:   "send.php",
   data: {
             options:  name-option,
             valueoptions:  value-option
            },

});


..... further in the send.php file I accept the POST array and sort through the knowledge there already, but the trouble is that I'm not correctly either collecting the array or passing it to the form.js file all the time a JS error crashes, what I'm doing wrong ? How is it done in this case?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-02-18
@thewind

data: $('form').serialize()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question