PHP - Single checkbox validation - validates but even when checked, won't submit. -
im writing code form validation in php, i've got problem checkbox validation. when going through form, if don't check checkbox, give correct error message.
however if check checkbox, still gives same error message.
here code far:
if (isset($_post['firstname'], $_post['lastname'], $_post['address'], $_post['email'], $_post['password'])) { $errors = array(); $firstname = $_post['firstname']; $lastname = $_post['lastname']; $address = $_post['address']; $email = $_post['email']; $password = $_post['password']; if (empty($firstname)) { $errors[] = 'first name can\'t empty'; } if (empty($lastname)) { $errors[] = 'last name can\'t empty'; } if (empty($address)) { $errors[] = 'address can\'t empty'; } if (filter_var($email, filter_validate_email) === false) { $errors[] = 'please enter valid email'; } if (empty($password)) { $errors[] = 'password can\'t empty'; } } if (!isset($checkbox)) { $errors[] = 'please agree privacy policy'; } $sex = $_post['sex']; $age = $_post['age']; $checkbox = $_post['checkbox']; $_session['validerrors'] = $errors; $_session['firstname'] = $firstname; $_session['lastname'] = $lastname; $_session['address'] = $address; $_session['email'] = $email; $_session['sex'] = $sex; $_session['age'] = $age; $_session['password'] = $password; $_session['checkbox'] = $checkbox; if (!empty($errors)) { header('location: index.php'); } else { header('location: writevalues.php'); }
everything else in above code working fine haven't been able find helpful answers checkbox validation situation. in advance!
you're calling code:
if (!isset($checkbox)) { $errors[] = 'please agree privacy policy'; }
before line:
$checkbox = $_post['checkbox'];
so of course isset($checkbox)
going return false
because not set @ point!
you change if statement to:
if(!isset($_post['checkbox'])){ ...
or move line $checkbox = $_post['checkbox'];
above if statement.
Comments
Post a Comment