7

I have a jQuery form in which I create a series of checkboxes:

<?php
<form method="post" id="b-form" action="../createb.php">
 for ($i=0; $i<$request_count; $i++){
 <div class="request-check">
 <table>
 <tr>
 <td><input type="checkbox" name="show_request[]" value="request".$i." checked="checked"/>select request</td>
 </tr>
 </table>
 </div>
 }

javascript

$.ajax({
 type: 'POST',
 url: '../createb.php',
 data: $('#b-form').serialize(),
 success: function (msg){
 alert(msg);
 }
})

at the moment createb.php is just testing the form

 $requests = $_POST['show_request'];
 $request_count = count($requests);
 echo 'count: '.$request_count;
 echo $requests[0];

The Problem is that the serialize function only sees the first checkbox and indicates whether it has been checked or not. It does not see any of the other check boxes. Does anybody have an idea why the other check boxes do not get serialized and what to do about it?

Thanks David

Gabriele Petrioli
197k35 gold badges276 silver badges331 bronze badges
asked Mar 22, 2011 at 9:23
1
  • Can you also paste here the HTML code generated by your script? The code you have pasted here looks strange to me?! Commented Mar 22, 2011 at 9:29

4 Answers 4

5

Quote from Jquery documentation: Only "successful controls" are serialized to the string (when using $(form).serialize()) or to array($(form).serializeArray()). Values from checkboxes and radio buttons (inputs of type "radio" or "checkbox") are included only if they are checked.

You can use something like this to emulate behavior similar to what you expect:

var myCbValuesArray = $("input:checkbox").map(function(){
 return $(this).is(":checked");
}).get();
answered Jul 12, 2014 at 18:50
Sign up to request clarification or add additional context in comments.

Comments

4

Your selector is wrong.

Try :

$('#b-form input:checkbox').serialize()

For get only checked input, use :

$('#b-form input:checkbox:checked').serialize()
answered May 23, 2013 at 5:17

Comments

3

Well, that's true as you are having all checkboxes with the name name and any id to separate them.

try to create them like:

<input 
 type = "checkbox" 
 id = "ckb_<?php echo $i ?>"
 name = "ckb_<?php echo $i ?>_show_request[]" 
 value = "request".$i."
 checked = "checked"/> select request

I'm sure that $('#b-form').serialize() will now generate all checkboxes as you want them

answered Mar 22, 2011 at 9:48

Comments

1

This will work for you.

You can get selected checkboxes values by

 $("input[type='checkbox']:checked").serialize();
answered Jan 1, 2016 at 10:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.