Programming

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.

Tags: , , , ,

6 Responses to “Submit unchecked checkbox value”

  1. On December 27, 2011 at 2:32 am cipmang responded with... #

    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. On May 1, 2012 at 6:14 am Rohit responded with... #

    Great man soo simple but powe full its working

  3. On September 21, 2012 at 1:24 am sridhar responded with... #

    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…

  4. On April 23, 2013 at 12:26 pm gnanasekar.s responded with... #

    worked for me

  5. On July 26, 2013 at 11:45 am Jbizzle responded with... #

    Worked great! Thanks!

  6. On March 12, 2014 at 7:09 pm Daniel Leal responded with... #

    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

Add your response