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?

Community
  • 1
  • 1
Martin Broder
  • 325
  • 1
  • 7
  • 21

2 Answers2

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);
})​
Sender
  • 6,660
  • 12
  • 47
  • 66
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.

Zut
  • 639
  • 1
  • 5
  • 12