2

Possible Duplicate:
Post the checkboxes that are unchecked

I've got a problem with serializing empty array inputs. for example:

<input type="checkbox" name="test_checkbox[]" value="test" id="test">
<input type="checkbox" name="test_checkbox[]" value="test1" id="test1">
<input type="checkbox" name="test_checkbox[]" value="test2" id="test2">

when none of these are checked and I submit the form (via POST), jQuery.serialize() excludes them and they do not appear in my $_POST/Serialized String.

But I need something like test_checkbox=0, when none is ticked.

How can I do that?

0

2 Answers 2

0

Simply inserting this in your server code

$_POST['test_checkbox'] = isset($_POST['test_checkbox']) ? $_POST['test_checkbox'] : 0;

Would leave you with the $_POST['test_checkbox'] variable set to 0 if none of them was set by the client. I'd recommend using a new variable instead of resetting the entry in $_POST, though.

0

try this way DEMO

   <form id="testform">
        <input type="checkbox" name="test_checkbox[0]" id="test" checked="checked" value="true"/>
        <input type="checkbox" name="test_checkbox[1]" id="test1" checked="checked" value="true"/>
    <span>click</span>
    </form>

script

$('span').click(function() {
    var formDate = $('#testform').serialize();
    console.log(formDate);
})​

Now You Want Like This Way See DEMO

$('span').click(function() {
     var str=$('form input:not([type="checkbox"])').serialize();
    var str1=$("form input[type='checkbox']").map(function(){return this.name+"="+this.checked;}).get().join("&");
    if(str1!="" && str!="") str+="&"+str1;
    else str+=str1; 
    alert(str);
})​
3
  • untick the checkboxes and I get console log "empty string". thats not what I wanted :) Sep 25, 2012 at 10:49
  • now check this DEMO
    – Sender
    Sep 25, 2012 at 10:59
  • Try this way also FIDDLE
    – Sender
    Sep 25, 2012 at 11:10

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