Submit unchecked checkbox value

I had never noticed before that submitting a form with unchecked checkboxes in it results in those checkboxes not being sent to the server at all. I had always assumed that a zero ( 0 ) would be sent for unchecked since a one ( 1 ) is sent when checked. Rather than add code to specifically handle these individual checkboxes, you can just add hidden inputs above the checkboxes in the HTML with a value of zero like this:

<input type="hidden" name="box1" value="0" /> <input type="checkbox" name="box1" value="1" />

This is a much simpler solution that doesn’t require writing any additional code or sending an array of checkboxes. I chose this method since I was using an automated Settings saver in PHP so it would be a pain to modify it and it could make the code more dirty.

Thanks to iamcam for writing about this.

6 thoughts on “Submit unchecked checkbox value”

  1. can you help me ?

    how about get the value of unchecked in array…. (i mean get from many checkbox…)

    the example above just use the same name, how if used array….

    thanks before…

  2. HI I tried with placing the input hidden but it doesn’t work at all.. Iam getting both the values.. It should be override when you checked the checkbox and when u uncheck the checkbox.. I tested it with creating one sample html page..

    If you have the sample then please provide me.. My outcome should be if I check the chekcbox it should get the value either yes/1 if I uncheck then it should get the values has either No/0…

  3. Excelent solution, if someone is interested in how it works.

    case checkbox checked: it sends checkbox value=1
    case checkbos unchecked: it sends hidden field value=0

    works flawlessly. Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *