Submit an HTML form with empty checkboxes Submit an HTML form with empty checkboxes php php

Submit an HTML form with empty checkboxes


I've used this technique from time to time:

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

note: This gets interpreted differently in different server-side languages, so test and adjust if necessary. Thanks to SimonSimCity for the tip.


Unchecked radio or checkbox elements are not submitted as they are not considered as successful. So you have to check if they are sent using the isset or empty function.

if (isset($_POST['checkbox'])) {    // checkbox has been checked}


An unchecked checkbox doesn't get sent in the POST data.You should just check if it's empty:

if (empty($_POST['myCheckbox']))     ....else     ....

In PHP empty() and isset() don't generate notices.