2011-08-17 42 views
0

我在設計一個註冊表單。我想在我的表單中添加一些驗證。驗證與需要字段驗證相關。這是我的代碼。如何在適當的地方在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> 

這是輸出屏幕來檢查驗證。這些錯誤信息即將發佈。但我希望這個錯誤必須在每條線的前面。例如,如果用戶不輸入電子郵件,則錯誤必須顯示在電子郵件文本框的前面。請建議我該怎麼做..?

+0

您可以爲每個表單字段創建一個錯誤消息變量,並在輸入字段旁邊回顯該變量的值。 –

+1

您的代碼存在各種問題:使用不推薦的方法('eregi'而不是'preg_match')引用某些數組鍵,** SQL INJECTION **,因爲您不會將數據轉義轉換爲SQL查詢。請啓用'error_reporting(E_ALL);'並閱讀有關安全PHP編程的內容! – ThiefMaster

回答

1

那麼如果你想讓錯誤出現在正確的表單字段前面,你將不知何故必須在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,它是deprecatad。改爲使用preg_match

+0

謝謝hoppa先生...非常感謝你... –

+0

先生請幫我在我的問題http://stackoverflow.com/questions/7077599/send-email-from-php我認爲你可以解決這個問題。 –

0

爲您驗證每個字段您可以創建你的錯誤,數組...

// 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()分裂通過逗號發送消息,爲你做任何事情。我認爲你會想爲每個領域收集多個錯誤。如果每個字段只需要一個錯誤,請考慮爲每個字段使用一個字符串而不是數組。