即使尚未提交,也会显示错误

时间:2015-06-10 16:11:26

标签: php error-handling required

我只是加载页面但错误已经显示

<?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>&nbsp;</td>
          <td>&nbsp;</td>
          <td><input type="submit" value="Submit"/></td>
          
        </tr>
      </table>
    </form>
</div>
</body>
</html>

它完全没有错误,但是当我加载页面(提交按钮甚至没有点击)时,显示了密码错误。其他字段错误工作正常。

1 个答案:

答案 0 :(得分:1)

您的密码块在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,就像我在上面的代码中所做的那样。