Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have few checkbox like this,

<input type='checkbox' name='name[]' value='1' />
<input type='checkbox' name='name[]' value='2' />
<input type='checkbox' name='name[]' value='3' />
<input type='checkbox' name='name[]' value='4' />
<input type='checkbox' name='name[]' value='5' />

And my php processing code is below,

<?php
$check=$_POST['name'];

foreach($check as $arr){
//code for saving data in database
}

My problem is, I am trying to send the check box data via jquery/ajax. But I could not send those data to the php page as an array.

Please tell me I could I do that.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

You can just serialize() or serializeArray() the checkboxes and send them to PHP :

$.ajax({
    url  : 'processing.php',
    data : $('[name="name[]"]').serialize()
}).done(function(result) {
    // ta da
});

Note that only checked boxes will be serialized, as there is generally no need to do anything for unchecked boxes.

share|improve this answer
    
thanks, it works –  Mushfiqul Tuhin Dec 8 '13 at 18:20

View

        <input type='checkbox' name='name[]' class="checkBoxGroup" value='1' />
        <input type='checkbox' name='name[]' class="checkBoxGroup" value='2' />
        <input type='checkbox' name='name[]' class="checkBoxGroup" value='3' />
        <input type='checkbox' name='name[]' class="checkBoxGroup" value='4' />
        <input type='checkbox' name='name[]' class="checkBoxGroup" value='5' />

Script

        $ary=$(".checkBoxGroup").serializeArray();
        alert($ary[0]['value']);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.