C
C
Cheizer2018-02-18 14:10:55
JavaScript
Cheizer, 2018-02-18 14:10:55

How to pass the results of jquery each function as an array to php?

Friends, for the first time I encounter such a task, I look for examples on the net and I still can’t understand whether there is either each or arr

What I have:
There is an unknown number of option blocks, they have an option name and value

<div class="option">
<span class="current">Размер</span>	
<div class="value">XL</div>
</div>

<div class="option">
<span class="current">Количество</span>	
<div class="value">5</div>
</div>


Using jquery I get this:
$(".option").each(function(index, value) { 
  var thisoption = "";
  var thisvalue = "";
  
 thisoption += $(this).find('.current').text();
 thisvalue += $(this).find('.value').text();
 
 console.log(index + ': ' + thisoption +' = '+ thisvalue); 
});


Everything is OK, the console prints out, and that’s it, the mind is not enough for now :))
And then I need to transfer this ajax to php and I’ll sort it out myself

In AJAX I accept form fields like this:
$.ajax({
                                type: "POST",
                                url:   mailURL,
                                data: {
        option:   :(( 
                                },
                            })


For input order, I can accept and send fields in php. But I still don’t know how to transfer the OPTION array :( PAMAGITIA!!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2018-02-18
@Cheizer

var toSend = [];

$(".option").each(function(index, value) {
    var thisoption = "";
    var thisvalue = "";

    toSend.push({
        option: $(this).find('.current').text(),
        value: $(this).find('.value').text()
    });
});

sendToHandle();
function sendToHandle() {
    $.ajax({
        type: "POST",
        url:   mailURL,
        data: toSend
    })
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question