Skip to content
Home » News » Dealing with checkboxes in PHP

Dealing with checkboxes in PHP

When you have a checkbox in a form you set the value that is passed to the form processing script in the value field of the checkbox element. What always catches me out is that if there is no check in the box nothing is passed to the script.

For example:

<input name="approved" type="checkbox" value="1"/>

If this is checked there will be a $_POST[‘approved’] value of 1 passed to the script defined in the form action. If not $_POST[‘approved’] is not set. This can cause your script to fail so make sure you take this into account. E.g. in your PHP:

$approved = 0;
if(isset($_POST['approved'])){
    $approved = 1;
}