0

I have been trying to send an ajax request to a php page. I have had no problems for regular HTML field data, however I am unable to figure out the correct syntax to pass a html field by name, when the name is an array.

Here is an example of what I mean:

<input type='checkbox' name='selected[]' value='my value'>

There is several of these as they are generated dynamically.

I attempt to call it as such :

$.post('myProcess.php',{exportSelected:true,myValues:$("input[name='selected[]']").val()})
                .done(function(data){
                    console.log(data);              
});

This works perfectly fine if I pass it as a single value, eg selected without there []. I am unsure how to say "pass ALL the values of selected[] if they are checked"

I have found a couple examples of posting regular javascript arrays, however this does not seem to help in this case.

Lain
  • 2,166
  • 4
  • 23
  • 47
  • For multiple you have iterate using each function and store values in array. And then send those values as json string or just array of selected values – MJN Nov 28 '19 at 19:34
  • As @Manjunath says use a loop to store the values e.g : var selected = $('input[name="selected[]"]').map((i,e)=>{ if(e.checked){ return e.value; } }) console.log(selected); – stan chacon Nov 28 '19 at 21:48

0 Answers0