How to send the values of an array of checkboxes through Ajax using jQuery? How to send the values of an array of checkboxes through Ajax using jQuery? arrays arrays

How to send the values of an array of checkboxes through Ajax using jQuery?


This worked fine for me

<input type="checkbox" class="ids" name="ids[]" value="2"><input type="checkbox" class="ids" name="ids[]" value="3"><input type="checkbox" class="ids" name="ids[]" value="4"><input type="checkbox" class="ids" name="ids[]" value="5"><input type="checkbox" class="ids" name="ids[]" value="6"><div id="response"></div><button id="submit">Submit</button><script>$('#submit').click(function() {$.ajax({    url: "stub.php",    type: "post",    data: $('.ids:checked').serialize(),    success: function(data) {    $('#response').html(data);    }});});</script>

Then on stub.php

var_dump($_POST);


Why don't you send the id's as comma separated string. You can split it on server side and apply the logic associated with it..

var ids = [];$('.ids:checked').each(function(i, e) {    ids.push($(this).val());});$.ajax({    url: "stub",    type: "post",    dataType: "json",    data: {        'ids[]': ids.join()    },    success: function(data) {        // stub    }});


Solution for me working fine

//get checkbox checked data as the arrayvar ids = new Array();      $('input[name="ids[]"]:checked').each(function(){         ids.push($(this).val());      });var dataString = 'ids='+ ids; $.ajax({            type: "POST",            url: "ajax_fees_pay_Directly.php",            data: dataString,            cache: false,            success: function(data){} });