Validating a single checkbox : Form Checkbox « Form « PHP






Validating a single checkbox

 
<?php
$value = 'yes';
echo "<input type='checkbox' name='subscribe' value='yes'/> Subscribe?";

if (isset($_POST['subscribe'])) {
    if ($_POST['subscribe'] == $value) {
        $subscribed = true;
    } else {
        $subscribed = false;
        print 'Invalid checkbox value submitted.';
    }
} else {
    $subscribed = false;
}

if ($subscribed) {
    print 'You are subscribed.';
} else {
    print 'You are not subscribed';
}
  
  








Related examples in the same category

1.Responding to Checkboxes
2.A form with checkboxes using the same name to store multiple values
3.Checking input from a checkbox or a multiple select
4.Using Default Checkbox Values