Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-单复选框验证-验证,但即使选中,也会赢得';不要屈服。_Php_Validation_Checkbox - Fatal编程技术网

PHP-单复选框验证-验证,但即使选中,也会赢得';不要屈服。

PHP-单复选框验证-验证,但即使选中,也会赢得';不要屈服。,php,validation,checkbox,Php,Validation,Checkbox,我正在用PHP编写表单验证代码,但我在复选框验证方面遇到了一个问题。 检查表单时,如果不选中复选框,它将给出正确的错误消息 但是,即使您选中了复选框,它仍然会给出相同的错误消息 以下是迄今为止的代码: if (isset($_POST['firstname'], $_POST['lastname'], $_POST['address'], $_POST['email'], $_POST['password'])) { $errors = array(); $firstname

我正在用PHP编写表单验证代码,但我在复选框验证方面遇到了一个问题。 检查表单时,如果不选中复选框,它将给出正确的错误消息

但是,即使您选中了复选框,它仍然会给出相同的错误消息

以下是迄今为止的代码:

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 be empty'; 
    }

    if (empty($lastname)) {
        $errors[] = 'Last name can\'t be empty'; 
    }

    if (empty($address)) {
        $errors[] = 'Address can\'t be empty';
    }

    if (filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) {
        $errors[] = 'Please enter a valid email'; 
    }

    if (empty($password)) {
        $errors[] = 'Password can\'t be empty'; 
    }

}

if (!isset($checkbox)) {
        $errors[] = 'Please agree to the 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'); 
}

上面代码中的其他内容都很好,但是我还没有找到任何关于复选框验证情况的有用答案。提前谢谢

您正在调用此代码:

if (!isset($checkbox)) {
        $errors[] = 'Please agree to the privacy policy';
} 
在这一行之前:

$checkbox = $_POST['checkbox'];
所以当然
isset($checkbox)
将返回
false
,因为它没有设置在那个点上

您可以将if语句更改为:

if(!isset($_POST['checkbox'])){ ...

或者移动行
$checkbox=$\u POST['checkbox']在if语句上方。

您正在调用此代码:

if (!isset($checkbox)) {
        $errors[] = 'Please agree to the privacy policy';
} 
在这一行之前:

$checkbox = $_POST['checkbox'];
所以当然
isset($checkbox)
将返回
false
,因为它没有设置在那个点上

您可以将if语句更改为:

if(!isset($_POST['checkbox'])){ ...

或者移动行
$checkbox=$\u POST['checkbox']在该if语句上方。

您正在检查在分配itAdd
exit()之前是否未设置
$checkbox
在所有标题位置之后。(您可能还希望在
index.php
的末尾取消设置
$\u会话['validerrors']
,否则其他页面也可能显示这些错误!)感谢您的帮助@Waygood-将所有这些添加到本页面和其他页面中,谢谢!您正在检查在分配itAdd
exit()之前是否未设置
$checkbox
在所有标题位置之后。(您可能还希望在
index.php
的末尾取消设置
$\u会话['validerrors']
,否则其他页面也可能显示这些错误!)感谢您的帮助@Waygood-将所有这些添加到本页面和其他页面中,谢谢!非常感谢!我现在觉得自己像个白痴!我会在10分钟后确定答案!非常感谢!我现在觉得自己像个白痴!我会在10分钟后确定答案!