santa
santa

Reputation: 12512

Trying to submit multiple checkbox values via ajax (jQuery)

I have a page with multiple checkboxes. I need to select all and submit via ajax.

function SelectedContactsMult(actMult) {    
    var data = { "delCont[]" : [] };
    $(".chkbx").each(function() {
        data["delCont[]"].push($(this).val());
    });     
    $.post("ajax.php?op="+actMult, data);
}

I get to my ajax page OK but I don't think I get any values... What am I missing here?

Upvotes: 1

Views: 4739

Answers (2)

tvanfosson
tvanfosson

Reputation: 532435

Let's assume that you have actual checkbox inputs such as:

<input type="checkbox" name="delCont[0]" class="chkbox" /> Choice 1
<input type="checkbox" name="delCont[1]" class="chkbox" /> Choice 2
...

You can then serialize them as:

function SelectedContactsMult(actMult) {        
    $.post("ajax.php?op="+actMult, $('.chkbox').serialize() );
}

Note it's important that each of the checkboxes include the "id" of the choice, since unchecked checkboxes won't be posted back and you'll need to be able to distinguish which checkboxes are checked. The names could be related to the choice, not a generic "array" notation, but it's important to know which ones, not just that some number have been selected.

Upvotes: 1

Rafay
Rafay

Reputation: 31033

var arr=[];

$.each($("input[type='checkbox']"),function(){
    arr.push($(this).val());
});

    $.post("ajax.php?op="+actMult, {data:arr});

Upvotes: 3

Related Questions