必填字段错误检查不起作用

时间:2013-07-10 03:18:38

标签: php mysql forms error-handling

正如标题所说我正在构建一个注册页面,我正在按照教程和构建代码进行操作,并且由于某些原因而不是打印错误,它只是在url中发布,这是我的代码。 / p>

    <?php include ("core/init.php"); ?>
<?php include ("includes/overall/header.php"); 

if (empty($_POST) === false) {
$required_fields = array('username', 'password', 'password_again', 'email');
foreach($_POST as $key=>$value) {
    if (empty($value) && in_array($key, $required_fields) === true) {
        $errors[] = 'Fields marked with an * are required';
        break 1;
    }

}


}
print_r($errors);
?>
<h1>Register</h1>
<form action="" method"post">
<ul>
    <li>
        Username*:<br>
        <input type="text" name="username">
    </li>
    <li>
        Password*:<br>
        <input type="password" name="password">
    </li>
    <li>
        Password Again*:<br>
        <input type="password" name="password_again">
    </li>
    <li>
        E-mail*:<br>
        <input type="text" name="email">
    </li>
    <li>
        <input type="submit" value="Register">
    </li>
</ul>

</form>
<?php include ("includes/overall/footer.php") ; ?>

1 个答案:

答案 0 :(得分:0)

<form action="" method"post">

应该是

<form action="" method="post">

您的表单默认为GET,因为它没有正确表示方法,因此$ _POST为空,因此您的foreach没有任何要检查的内容。