0

我创建了一个注册页面,用于检查错误并为用户打印出来。例如,如果他们没有有效的电子邮件地址,则会显示无效的电子邮件地址。在逻辑完成的顶部,它让网页知道是否有任何错误,以便稍后我可以无错误地注册它们。在我试图打印输出错误的 html 底部,它们显示在文本字段旁边。我创建了一些错误,但由于某种原因,它们在用户打印任何东西之前就打印出来了。代码如下:

<form action="" method="POST">
                    <input type="hidden" name="action" value="register" />
                    <?php if(@$_POST['action'] == 'register') {
                    }
                    <input type="text" name="firstname" maxlength = "16" class="text-bars" placeholder="Firstname"/>
                    <input type="text" name="lastname" maxlength = "16" class="text-bars" placeholder="Lastname"/><br /><br />
                    <input type="text" name="username" maxlength = "16" class="text-bars" placeholder="Username" />
                    <?php if(user_exists($_POST['username']) === true){
                            echo "Username already exsits.";
                            }else if(preg_match("/\\s/", $_POST['username']) == true){
                            echo 'Your username must not contain spaces.';
                            }
                    ?>
                    <br /><br />
                    <input type="password" name="password" maxlength = "16" class="text-bars" placeholder="Password" />
                    <input type="password" name="re_password" maxlength = "16" class="text-bars" placeholder="Re-Enter Password" />

                            if($_POST['password'] !== $_POST['re_password']){
                            echo "Your passwords do not match.";
                            }else if(strlen($_POST['password']) !== 0 && strlen($_POST['password']) < 6){
                            echo "Your password must be at least 6 characters.";
                            } 
                    ?>
                    <br /><br />
                    <input type="text" name="email" maxlength = "25" class="text-bars" placeholder="Email" />
                    <input type="text" name="re_email" maxlength = "25" class="text-bars" placeholder="Re-enter Email" />
                    <?php
                        if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false && $_POST['email'] !== 0){
                            echo "A valid email address is required.";
                        }else if(email_exists($_POST['email']) === true){
                            echo "Email address already in use.";
                        } else if($_POST['email'] !== $_POST['re_email']){
                            echo "Your emails do not match.";
                        }
                    ?>
                    <br /><br />
                    <input type="text" name="zipcode" maxlength = "7" class="text-bars" placeholder="Zip Code" /><br /><br />

                    <input type="submit" value="Submit" />
          </form>

检查密码的错误长度超过 6 个字符,并且电子邮件验证似乎在页面加载时正确打印。这个问题有什么解决办法吗?

感谢大家!

$errors = array();

if(empty($_POST) === false){
$required_fields = array('firstname', 'lastname', 'username', 'password', 're_password', 'email', 're_email', 'zipcode');
foreach($_POST as $key=>$value){
    if(empty($value) && in_array($key, $required_fields) === true){
        $errors[] = 'All fields are required';
        break 1;
    }
}

if(empty($errors) === true){
    if(user_exists($_POST['username']) === true){
        $errors[] = 'Username already exsits.';
        }
    if(preg_match("/\\s/", $_POST['username']) == true){
        $errors[] = 'Your username must not contain spaces.';
    }
    if(strlen($_POST['password']) < 6){
        $errors[] = 'Your password must be at least 6 characters.';
    }
    if($_POST['password'] !== $_POST['re_password']){
        $errors[] = 'Your passwords do not match.';
                            }
    if(strlen($_POST['password']) !== 0 && strlen($_POST['password']) < 6){
        $errors[] = 'Your password must be at least 6 characters.';
                            }
    if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
        $errors[]= 'A valid email address is required.';
    }
    if(email_exists($_POST['email']) === true){
        $errors[] = 'Email address already in use.';
    } 
    if($_POST['email'] !== $_POST['re_email']){
        $errors[] = 'Your emails do not match.';
    }
}

}

4

4 回答 4

1

<form>在元素内的代码中添加隐藏的输入。

<input type="hidden" name="action" value="register" />

在您的 php 代码周围,使用以下内容

<?php if(@$_POST['action'] == 'register') {

} ?>

这将仅在用户提交表单时触发您的 php 代码。

这是您需要的确切代码

<?php
    if(@$_POST['action'] == 'register') {
        if(user_exists($_POST['username']) === true){
            $errorUser = "Username already exsits.";
        }else if(preg_match("/\\s/", $_POST['username']) == true){
            $errorUser = 'Your username must not contain spaces.';
        }

        if($_POST['password'] !== $_POST['re_password']){
            $errorPassword = "Your passwords do not match.";
        }else if(strlen($_POST['password']) !== 0 && strlen($_POST['password']) < 6){
            $errorPassword = "Your password must be at least 6 characters.";
        }

        if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false && $_POST['email'] !== 0){
            $errorEmail = "A valid email address is required.";
        }else if(email_exists($_POST['email']) === true){
            $errorEmail = "Email address already in use.";
        } else if($_POST['email'] !== $_POST['re_email']){
            $errorEmail = "Your emails do not match.";
        }
    }
?>

<form action="" method="POST">
    <input type="hidden" name="action" value="register" />
    <input type="text" name="firstname" maxlength = "16" class="text-bars" placeholder="Firstname"/>
    <input type="text" name="lastname" maxlength = "16" class="text-bars" placeholder="Lastname"/>
    <?=@$errorUser;?>
    <br /><br />
    <input type="text" name="username" maxlength = "16" class="text-bars" placeholder="Username" />
    <br /><br />
    <input type="password" name="password" maxlength = "16" class="text-bars" placeholder="Password" />
    <input type="password" name="re_password" maxlength = "16" class="text-bars" placeholder="Re-Enter Password" />
    <?=@$errorPassword;?>
    <br /><br />
    <input type="text" name="email" maxlength = "25" class="text-bars" placeholder="Email" />
    <input type="text" name="re_email" maxlength = "25" class="text-bars" placeholder="Re-enter Email" />
    <?@$errorEmail;?>
    <br /><br />
    <input type="text" name="zipcode" maxlength = "7" class="text-bars" placeholder="Zip Code" /><br /><br />
    <input type="submit" value="Submit" />
</form>
于 2013-05-02T16:29:32.797 回答
0

您的问题是无论表单是否已提交,验证都会完成。我建议您在 html 之前在 PHP 中进行所有验证,您可以使用数组来捕获错误并在表单中打印出来。这将使您的逻辑和内容在更大程度上保持分离,从而使调试更容易。

<?php
if ($_POST) { //Check that the form has actually been submitted
    $errors = array();
    if(user_exists($_POST['username']) === true) {
        $errors['user'] == 'Username exists';
    }
    ... // more error checking
}

... //form output
<input type="text" name="username" maxlength = "16" class="text-bars" placeholder="Username" />
<?php print(isset($errors['username'])) ? $errors['username'] : ''; ?>
... //more form output

这应该让你开始

于 2013-05-02T16:33:06.013 回答
0

一、声明错误

$errors = array();

每次发现错误时,将其附加到变量中

$errors[] = 'Your Error Text';

最后,打印错误

echo '<ul>';
foreach($errors as $error) {
    echo "<li>$error</li>";
}
echo '</ul>';
于 2013-05-02T16:38:17.840 回答
0

我建议您在提交后在单独的代码块中验证用户输入,例如在脚本的顶部:

if (isset($_POST("submit")) {  // form has ben submitted...

    // validate
    if (!isset($_POST['firstname']) || strlen($_POST['firstname']) == 0)
        $error['firstname'] = "Please enter a first name!";

    // do this with all your fields

    if (!isset($error)) { // no error = all fields valid

        // save fields to database
        // route to another page
        exit();
    } // if !isset $error

} // if isset

以你的形式,你...

<form name="myform" method="post">
<?php=(isset($error)?'There were errors:':'')?>
<?php $fieldname = 'firstname'; ?>
<?php=(isset($error[$fieldname])?$error[$fieldname].'<br />':'')?>
<input type="text" name="firstname" maxlength="16" 
    class="text-bars<?php=(isset($error[$fieldname])?' error':'')?>" 
    value="<?php=(isset($_POST[$fieldname])?$_POST[$fieldname]:'')?>"/>

<!-- do the same for all other fields... -->

<input type="submit" name="submit" value="submit" /> <!-- add name attribute! -->
</form>

像这样,您正在显示一条错误消息... - 如果设置
在表单 顶部 - 在包含错误的字段顶部如果设置... -字段的属性设置为,所以如果您定义了一个类错误,您可以突出显示该字段。$error
$error[fieldname]
classtext-bars error

于 2013-05-02T16:42:30.463 回答