还没提交就显示错误
error for displayed even it has not been submitted yet
我刚加载页面就显示错误
<?php
$nameErr = $emailErr = $idErr = $passwordErr = "";
$name = $email = $id = $password = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["id"])) {
$genderErr = "Matric number is required";
} else {
$website = test_input($_POST["id"]);
if (!preg_match("/^[0-9].*$/",$id)){
$idErr = "Only numbers allowed"; }
}
}
if (empty($_POST["password"])) {
$passwordErr = "Password is required";
} else {
$password = test_input($_POST["password"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z0-9]+$/",$password)) {
$nameErr = "Special characters are not allowed";
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Pendaftaran Kali Pertama</title>
<style type="text/css">
<!--
.style1 {
color: #FFCC00;
font-weight: bold;
}
.error{ color:#FFCC00}
-->
</style>
</head>
<body>
<div align="center">
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table width="62%" border="0" cellpadding="2" cellspacing="2">
<tr>
<td height="35" colspan="4" bgcolor="#660066"><div align="center" class="style1">PENDAFTARAN KALI PERTAMA </div></td>
</tr>
<tr>
<td width="33%"><div align="right"><strong>Nama </strong></div></td>
<td width="5%"><div align="center"><strong>:</strong></div></td>
<td width="62%"><input name="name" type="text" size="50" /><span class="error">* <?php echo $nameErr;?></span></td>
</tr>
<tr>
<td><div align="right"><strong>No. Pelajar </strong></div></td>
<td><div align="center"><strong>:</strong></div></td>
<td><input name="id" type="text" size="50" maxlength="10" /><span class="error">* <?php echo $idErr;?></span></td>
</tr>
<tr>
<td><div align="right"><strong>Kata Laluan(8 aksara sahaja)</strong></div></td>
<td><div align="center"><strong>:</strong></div></td>
<td><input name="password" type="text" size="50" maxlength="8" /><span class="error">* <?php echo $passwordErr;?></span></td>
</tr>
<tr>
<td><div align="right"><strong>Emel</strong></div></td>
<td><div align="center"><strong>:</strong></div></td>
<td><input name="email" type="text" size="50" /><span class="error">* <?php echo $emailErr;?></span></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" value="Submit"/></td>
</tr>
</table>
</form>
</div>
</body>
</html>
完全没有错误,但是当我加载页面时(甚至没有点击提交按钮),密码错误已经显示。其他字段错误工作正常。
您的密码块在 POST 块之外,这就是为什么 $passwordErr
总是在提交表单之前设置的原因。
if (empty($_POST["password"])) {
$passwordErr = "Password is required";
} else {
$password = test_input($_POST["password"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z0-9]+$/",$password)) {
$passwordErr = "Special characters are not allowed";
}
}
把它放在方块里面if ($_SERVER["REQUEST_METHOD"] == "POST") {
同样在您的代码中 $nameErr = "Special characters are not allowed";
是错误的。您必须像我在上面的代码中所做的那样将错误分配给 $passwordErr。
我刚加载页面就显示错误
<?php
$nameErr = $emailErr = $idErr = $passwordErr = "";
$name = $email = $id = $password = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["id"])) {
$genderErr = "Matric number is required";
} else {
$website = test_input($_POST["id"]);
if (!preg_match("/^[0-9].*$/",$id)){
$idErr = "Only numbers allowed"; }
}
}
if (empty($_POST["password"])) {
$passwordErr = "Password is required";
} else {
$password = test_input($_POST["password"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z0-9]+$/",$password)) {
$nameErr = "Special characters are not allowed";
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Pendaftaran Kali Pertama</title>
<style type="text/css">
<!--
.style1 {
color: #FFCC00;
font-weight: bold;
}
.error{ color:#FFCC00}
-->
</style>
</head>
<body>
<div align="center">
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table width="62%" border="0" cellpadding="2" cellspacing="2">
<tr>
<td height="35" colspan="4" bgcolor="#660066"><div align="center" class="style1">PENDAFTARAN KALI PERTAMA </div></td>
</tr>
<tr>
<td width="33%"><div align="right"><strong>Nama </strong></div></td>
<td width="5%"><div align="center"><strong>:</strong></div></td>
<td width="62%"><input name="name" type="text" size="50" /><span class="error">* <?php echo $nameErr;?></span></td>
</tr>
<tr>
<td><div align="right"><strong>No. Pelajar </strong></div></td>
<td><div align="center"><strong>:</strong></div></td>
<td><input name="id" type="text" size="50" maxlength="10" /><span class="error">* <?php echo $idErr;?></span></td>
</tr>
<tr>
<td><div align="right"><strong>Kata Laluan(8 aksara sahaja)</strong></div></td>
<td><div align="center"><strong>:</strong></div></td>
<td><input name="password" type="text" size="50" maxlength="8" /><span class="error">* <?php echo $passwordErr;?></span></td>
</tr>
<tr>
<td><div align="right"><strong>Emel</strong></div></td>
<td><div align="center"><strong>:</strong></div></td>
<td><input name="email" type="text" size="50" /><span class="error">* <?php echo $emailErr;?></span></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" value="Submit"/></td>
</tr>
</table>
</form>
</div>
</body>
</html>
完全没有错误,但是当我加载页面时(甚至没有点击提交按钮),密码错误已经显示。其他字段错误工作正常。
您的密码块在 POST 块之外,这就是为什么 $passwordErr
总是在提交表单之前设置的原因。
if (empty($_POST["password"])) {
$passwordErr = "Password is required";
} else {
$password = test_input($_POST["password"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z0-9]+$/",$password)) {
$passwordErr = "Special characters are not allowed";
}
}
把它放在方块里面if ($_SERVER["REQUEST_METHOD"] == "POST") {
同样在您的代码中 $nameErr = "Special characters are not allowed";
是错误的。您必须像我在上面的代码中所做的那样将错误分配给 $passwordErr。