William William - 1 year ago 75
PHP Question

checkbox value broken if statment

I have a check box that I am trying to make required, the name age_agree

the value in html "Yes"

then with my other form errors

if(isset($_POST['age_agree']) && $_POST['age_agree'] == 'No') {
$errors[] = "You must agree you are 18 years of age.";

it don't give any error but allows the boxes to be unchecked

Answer Source

Remember if a checkbox is not checked it is not sent to the PHP in $_POST, so its actual value is almost irrelevant, all you need to do is test for the existance of the occurance in the $_POST array

if( ! isset($_POST['age_agree']) ) {
    $errors[] = "You must agree you are 18 years of age.";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download