如何在php中适当的位置添加验证


How to add validation in php at proper place?

我正在设计一个php注册表单。我想在表单中添加一些验证。验证与需求字段验证相关。这是我的代码。

<?php
   include('connect.php');
    if(isset($_REQUEST['Register']))
    {
         $error='';//initialize $error to blank
  if(trim($_POST[userid])==''){
      $error.="Please enter a username!<br />"; //concatenate the $error Message with a line break 
  }
  if(trim($_POST[password])==''){
      $error.="Please Enter password! <br />";//concatenate more to $error  
  } 
  if(trim($_POST[email])=='')
  {
    $error.="Please Enter email address !<br />"; 
  }
  else
   {
        if(!eregi("^[_a-z0-9-]+('.[_a-z0-9-]+)*@[a-z0-9-]+('.[a-z0-9-]+)*('.[a-z]{2,3})$", $_POST[email])) { 
        $error="The e-mail you entered was not in the proper format!"; 
        }
    }
    if(trim($_POST[regAs])=='NULL')
  {
    $error.="Please select Register As !<br />"; 
  }
  if($error!='')
  {
    echo "<span style=color:red>$error</span>";
    //Hmmmm no text is in $error so do something else, the page has verified and the email was valid
    // so uncomment the line below to send the user to your own success page or wherever (swap  
     //yourpage.php with your files location). 
     //echo "script type='"text/javascript'">window.location='yourpage.php'"<script>";  
   }
   else
   {
           $userid=$_REQUEST['userid'];
        $name=$_REQUEST['name'];
       $password=$_REQUEST['password'];
       $repassword=$_REQUEST['repassword'];
       $email=$_REQUEST['email'];
       $regAs=$_REQUEST['regAs'];
       if($password!=$repassword)
        {
           echo "<script>alert('both are diferent password')</script>";
        }
       else
        {
         mysql_query("insert into login(userid,name,password,email,regAs)  values('$userid','$name','$password','$email','$regAs')");
        }   
   }                





    }
?>
<html>
<head></head>
<body>
<form action="" method="post" name="f">
<table align="center">
<tr>
<td>User ID :</td><td>
<input type="text" name="userid" value=""></td>
</tr>
<tr>
<td>Name :</td><td>
<input type="text" name="name" value=""></td>
</tr>
<tr>
<td>Password : </td>
<td><input type="password" name="password" value=""></td>
</tr>
<tr>
<td>Re-Enter Password : </td>
<td><input type="password" name="repassword" value=""></td>
</tr>
<tr>
<td>Email id :</td><td>
<input type="text" name="email" value=""></td>
</tr>
<tr>
<td>Register As :</td><td>
<select id="regAs" name="regAs">
<option value="NULL">SELECT</option>
<option value="Artist">Artist</option>
<option value="Buyer">Buyer</option>
</select></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Register" value="Register"</td></tr>
</table>
</form>
</body>
</html>

,这是检查验证的输出屏幕。这些错误消息出现在第一行。但是我希望错误必须出现在每一行的前面。例如,如果用户不输入电子邮件,那么错误必须显示在电子邮件文本框前面。请告诉我该怎么办?

如果你想让错误出现在正确的表单字段前面,你就必须在HTML中正确的位置得到错误。最简单的解决方案是为每个可能的表单字段创建一个变量或数组节点,并在必要时向其中插入一条错误消息。我举一个简短的例子:

<?php
include('connect.php');
if(isset($_REQUEST['Register'])) {
    $errors = array(
        'email'    => '',
        'username' => '',
        //etc...
    );
    if(!eregi("^[_a-z0-9-]+('.[_a-z0-9-]+)*@[a-z0-9-]+('.[a-z0-9-]+)*('.[a-z]{2,3})$", $_POST[email])) { 
        $error['email'] = "The e-mail you entered was not in the proper format!"; 
    }
}
?>

然后在你的HTML中:

<tr>
  <td>Email id :</td>
  <td>
    <input type="text" name="email" value="">
    <?php echo $error['email']; ?>
  </td>
</tr>

此外,不要使用eregi,它已被弃用。使用preg_match代替。

您可以在验证每个字段时创建一个包含错误的数组…

// Might as well define them all so we don't have to use isset() later
// This isn't necessary, it just makes the echo logic easier
foreach ($_POST as $k => $v) {
    $errors[$k] = array(); // No error yet
}
if ( ! valid_email($_POST['email'])) {
    // Add an error to the array
    $errors['email'][] = 'The email address is not valid.';
}

…然后在HTML中返回错误:

<tr>
  <td>Email id :</td>
  <td>
    <input type="text" name="email" value="">
    <?php echo implode(', ', $errors['email']); ?>
  </td>
</tr>

我使用implode()通过逗号分割消息,做任何适合您的工作。我假设您希望为每个字段收集多个错误。如果每个字段只需要一个错误,请考虑对每个字段使用字符串而不是数组。