我已经在我的注册页面上创建了验证表单输入的代码,然后将页面帖子提交回来自html表单的输入数据。然后,php验证用户是否在所有框中输入输入,还指定了ises字符和空白区域,电子邮件格式正确,密码长于6个字符,并将密码与确认密码进行比较。
如果测试失败,页面会显示各种错误。一切正常。我的问题是,即使验证抛出错误并显示错误,它也不会阻止它被发布到下一页,然后将数据添加到mysql数据库。我无法弄清楚如何让错误报告停止发送数据,直到输入正确,我已添加下面的相关PHP。
任何帮助都会得到最好的回复,我是php的新手,所以不了解很多方面,但已经看了很多资源试图解决这个问题而没有成功。此外,我想在发布到下一个php进程页面之前对密码进行哈希处理,但是我对我读过的许多资源的内容感到非常困惑。
<?php
// define variables and set to empty values
$usernameErr = $emailErr = $passwordErr = $clubnameErr = "";
$username = $email = $password = $clubname = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$password = test_input($_POST["website"]);
$clubname = test_input($_POST["comment"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["username"])) {
$usernameErr = "**Name is required**";
} else {
$username = test_input($_POST["username"]);
if (!preg_match("/^[a-zA-Z ]*$/",$username)) {
$usernameErr = "**Only letters and white space allowed**";
}
}
if (empty($_POST["email"])) {
$emailErr = "**Email is required**";
} else {
$email = test_input($_POST["email"]);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
$emailErr = "**Invalid email format**";
}
}
if (empty($_POST["password"])) {
$passwordErr = "**Password is required**";
} else {
$password = test_input($_POST["password"]);
}
if (empty($_POST["confirmpassword"])) {
$confirmpasswordErr = "**Please confirm password**";
} else {
$confirmpassword = test_input($_POST["confirmpassword"]);
}
if($_POST['password'] != $_POST['confirmpassword']){
$confirmpasswordErr = "**Passwords did not match**";
}
if($_POST["password"] && strlen(trim($_POST["password"])) < 6)
{
$passwordErr = "**Password needs to be at least 6 characters**";
}
if (empty($_POST["clubname"])) {
$clubnameErr = "**Club Name is required**";
} else {
$clubname = test_input($_POST["clubname"]);
if (!preg_match("/^[a-zA-Z ]*$/",$clubname)) {
$clubnameErr = "**Only letters and white space allowed**";
}
}
}
=============================================== ============================================ 完整的页面代码
include_once 'includes/register.inc.php';
// define variables and set to empty values
$usernameErr = $emailErr = $passwordErr = $clubnameErr = "";
$username = $email = $password = $clubname = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$password = test_input($_POST["website"]);
$clubname = test_input($_POST["comment"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["username"])) {
$usernameErr = "**Name is required**";
} else {
$username = test_input($_POST["username"]);
if (!preg_match("/^[a-zA-Z ]*$/",$username)) {
$usernameErr = "**Only letters and white space allowed**";
}
}
if (empty($_POST["email"])) {
$emailErr = "**Email is required**";
} else {
$email = test_input($_POST["email"]);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
$emailErr = "**Invalid email format**";
}
}
if (empty($_POST["password"])) {
$passwordErr = "**Password is required**";
} else {
$password = test_input($_POST["password"]);
}
if (empty($_POST["confirmpassword"])) {
$confirmpasswordErr = "**Please confirm password**";
} else {
$confirmpassword = test_input($_POST["confirmpassword"]);
}
if($_POST['password'] != $_POST['confirmpassword']){
$confirmpasswordErr = "**Passwords did not match**";
}
if($_POST["password"] && strlen(trim($_POST["password"])) < 6)
{
$passwordErr = "**Password needs to be at least 6 characters**";
}
if (empty($_POST["clubname"])) {
$clubnameErr = "**Club Name is required**";
} else {
$clubname = test_input($_POST["clubname"]);
if (!preg_match("/^[a-zA-Z ]*$/",$clubname)) {
$clubnameErr = "**Only letters and white space allowed**";
}
}
}
echo $_POST['username'];
echo $_POST['email'];
echo $_POST['password'];
echo $_POST['confirmpassword'];
echo $_POST['clubname'];
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style type="text/css">
#form {
width: 80%;
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<div id="form">
<form id="registermember" name="registermember" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div align="center">
<fieldset>
<legend style="font-weight: bolder; font-size: large;">Register Members</legend>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="33%"><div align="right"><span style="color: #000000; font-weight: bold;">Members Name:</span></div></td>
<td width="33%"><div align="center"><span style="color: #000000; font-weight: bold;">
<input name="username" type="text" id="username" value="<?php echo $username;?>" size="40%">
</span></div>
<td width="33%"><div align="left"><span class="error" style="color: #FF0004; font-weight: bold;"><?php echo $usernameErr;?></span></div></td>
</tr>
<tr>
<td><div align="center"><span style="color: #000000; font-weight: bold;">
</span></div> <span style="color: #000000; font-weight: bold;">
<label for="email">
<div align="center">
<div align="right"><span style="color: #000000; font-weight: bold;">Members Email:</span></div>
</div>
<span style="color: #000000; font-weight: bold;"></span><span style="color: #000000; font-weight: bold;">
</label>
</span></td>
<td><div align="center"><span style="color: #000000; font-weight: bold;">
<input name="email" type="text" id="email" value="<?php echo $email;?>" size="40%">
</span></div>
<td style="color: #FF0004"><div align="left"><span style="color: #FF0004; font-weight: bold;"><span class="error"><?php echo $emailErr;?></span></span></div></td>
</tr>
<tr>
<td><div align="center"><span style="color: #000000; font-weight: bold;">
</span></div> <span style="color: #000000; font-weight: bold;">
<label for="password2">
<div align="center">
<div align="right"><span style="color: #000000; font-weight: bold;">Members Password:</span></div>
</div>
<span style="color: #000000; font-weight: bold;"></span><span style="color: #000000; font-weight: bold;">
</label>
</span></td>
<td><div align="center"><span style="color: #000000; font-weight: bold;">
<input name="password" type="password" id="password" size="40%">
</span></div>
<td style="color: #FF0004"><div align="left"><span style="color: #FF0004; font-weight: bold;"><span class="error"><?php echo $passwordErr;?></span></span></div></td>
</tr>
<tr>
<td><div align="center"><span style="color: #000000; font-weight: bold;">
</span></div> <span style="color: #000000; font-weight: bold;">
<label for="confirmpassword">
<div align="center">
<div align="right"><span style="color: #000000; font-weight: bold;">Confirm Password:</span></div>
</div>
<span style="color: #000000; font-weight: bold;"></span><span style="color: #000000; font-weight: bold;">
</label>
</span></td>
<td><div align="center"><span style="color: #000000; font-weight: bold;">
<input name="confirmpassword" type="password" id="confirmpassword" size="40%">
</span></div>
<td style="color: #FF0004"><div align="left"><span style="color: #FF0004; font-weight: bold;"><span class="error"><?php echo $confirmpasswordErr;?></span></span></div></td>
</tr>
<tr>
<td><div align="center"><span style="color: #000000; font-weight: bold;">
</span></div> <span style="color: #000000; font-weight: bold;">
<label for="clubname2">
<div align="center">
<div align="right"><span style="color: #000000; font-weight: bold;">Members Club:</span></div>
</div>
<span style="color: #000000; font-weight: bold;"></span><span style="color: #000000; font-weight: bold;">
</label>
</span></td>
<td><div align="center"><span style="color: #000000; font-weight: bold;">
<input name="clubname" type="text" id="clubname" value="<?php echo $clubname;?>" size="40%">
</span></div>
<td style="color: #FF0004"><div align="left"><span style="color: #FF0004; font-weight: bold;"><span class="error"><?php echo $clubnameErr;?></span></span></div></td>
</tr>
<tr>
<td> </td>
<td><div align="center">
<p>
<input name="submitmember" type="submit" id="submitmember" formaction="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" formmethod="POST" value="Add Member">
</p>
</div>
<td style="color: #FF0004"> </td>
</tr>
</table>
</fieldset>
</form>
</div>
</body>
</html>