我创建了一个注册页面,用于检查错误并为用户打印出来。例如,如果他们没有有效的电子邮件地址,则会显示无效的电子邮件地址。在逻辑完成的顶部,它让网页知道是否有任何错误,以便稍后我可以无错误地注册它们。在我试图打印输出错误的 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.';
}
}
}