Zain
Zain

Reputation: 662

Sending each select value via javascript to php script

I'm using ajax function to send select values to my php script, it is working fine but i have issue when there user add multiple data, i have option to add multiple add child in the form, user can add as many fields. I need a each loop here. This is my code.

 <select class="firstpiller" name="firstpiller">
        <option value="0">0</option>
        <option value="1">1</option>
     </select>

     <select class="thirdpiller" name="thirdpiller">
        <option value="2">2</option>
        <option value="3">3</option>
     </select>

       if (document.querySelector('[name=firstpiller]') != null){
        var firstpiller = document.querySelector('[name=firstpiller]').value;
        } else {
           var firstpiller = null; 
        }


        if (document.querySelector('[name=thirdpiller]') != null){
        var thirdpiller = document.querySelector('[name=thirdpiller]').value;
        } else {
           var thirdpiller = null; 
        }


        var stepVar = firstpiller + "--" + thirdpiller;

Can anyone help how should i write each loop here?

Thanks in advance.

Upvotes: 0

Views: 35

Answers (1)

fayis003
fayis003

Reputation: 680

add a common class to the select say you are given a class .myselect, now you can do something like below

var stepvar = '';
$('.myselect').each(function(value){
    stepvar += '--' + value.val(); 
});
alert(stepvar);

Upvotes: 2

Related Questions