I have an HTML form:
<form action='process.php' method='post'> <input type='checkbox' name='check_box_1' /> Check me!<br> </form>
Here is a section from the PHP script
echo (isset($_POST['check_box_1']))?'Set':'Not set';
The output of the script when the checkbox is set is
But when the checkbox is not set, the output is:
Why is this? This seems like a very poor design because my PHP script checks a number of
$_POST variables to make sure they were passed along to the script. When the
$_POST['check_box_1'] value is not set, then how do I know whether the script failed to pass along the value or the checkbox was just not set?
If you want to overcome this design feature, try this:
<input type="hidden" name="check_box_1" value="0" /> <input type="checkbox" name="check_box_1" value="1" />
This way, you’ll always have
$_POST['check_box_1'] set in the callback page, and then you can just check its value to see whether they checked it or not. The two inputs have to be in that order though, since the later one will take precedence.