Friday, February 23, 2024
 Popular · Latest · Hot · Upcoming
188
rated 0 times [  195] [ 7]  / answers: 1 / hits: 112127  / 15 Years ago, tue, may 26, 2009, 12:00:00

How to pass multiple checkboxes using jQuery ajax post



this is the ajax function



 function submit_form(){
$.post(ajax.php, {
selectedcheckboxes:user_ids,
confirm:true
},
function(data){
$(#lightbox).html(data);
});
}


and this is my form



<form>
<input type='checkbox' name='user_ids[]' value='1'id='checkbox_1' />
<input type='checkbox' name='user_ids[]' value='2'id='checkbox_2' />
<input type='checkbox' name='user_ids[]' value='3'id='checkbox_3' />
<input name=confirm type=button value=confirm onclick=submit_form(); />
</form>

More From » jquery

 Answers
112

From the jquery docs for POST (3rd example):



$.post(test.php, { 'choices[]': [Jon, Susan] });


So I would just iterate over the checked boxes and build the array. Something like



var data = { 'user_ids[]' : []};
$(:checked).each(function() {
data['user_ids[]'].push($(this).val());
});
$.post(ajax.php, data);

[#99457] Thursday, May 21, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nadiaa

Total Points: 572
Total Questions: 113
Total Answers: 113

Location: Mauritania
Member since Sun, Oct 17, 2021
2 Years ago
nadiaa questions
Thu, Jun 25, 20, 00:00, 4 Years ago
Fri, May 29, 20, 00:00, 4 Years ago
Fri, Dec 20, 19, 00:00, 4 Years ago
;