2015-02-11 64 views
2

我有一個幾個領域的形式。我想爲每個缺少的字段顯示不同的錯誤消息。如果一個字段是空的,我會顯示一個jQuery對話框和正確填寫的初始表單。最好的方法來管理空場的錯誤

我做了這樣的事情:

if(isset($_POST['submit'])){ 

    if($_POST['email'] && $_POST['email'] != ''){ 

     if($_POST['password'] && $_POST['password']!= ''){ 

      if($_POST['password'] == $_POST['confirm_password']) 
      {    
        ... 
        ... 
      }      

      }else ?> <div class="dialog" id="dialog" title="Error"> <p>The passwords you entered do not match</p>  </div><?php 

     }else ?> <div class="dialog" id="dialog" title="Error"> <p>Password field is empty</p>  </div><?php 

    }else ?> <div class="dialog" id="dialog" title="Error"> <p>Email field is empty</p>  </div><?php 
}else {?> 

然後,我顯示我的形式

 <form method="post" action="submenu_signup.php"> 

      <table class="center"> 

       <tr> 
        <td width="50%" > Email : </td> 
        <td width="50%" > <input class="more_width" type="text" name="email" id="email" placeholder="Ex : [email protected]" maxlength="45" /></td> 
       </tr> 
       <tr> 
        <td width="50%" > Password : </td> 
        <td width="50%" > <input class="more_width" type="password" name="password" id="password" maxlength="40" /></td> 
       </tr> 
       <tr> 
        <td width="50%" > Confirm password : </td> 
        <td width="50%" > <input class="more_width" type="password" name="confirm_password" id="confirm_password" maxlength="40" /></td> 
       </tr> 
       <tr> 
        <td width="50%" > Service : </td> 
        <td width="50%" > <input class="more_width" type="text" name="service" id="service" placeholder="Ex : Neurosurgery, orthopedics, ..." maxlength="255" /></td> 
       </tr> 
       <tr> 
        <td width="50%" > Phone number : </td> 
        <td width="50%" > <input class="more_width" type="text" name="phone_number" id="phone_number" placeholder="Ex :" maxlength="20" /></td> 
       </tr> 
       <tr> 
        <td colspan="2" > <input class="submit" type="submit" name="submit" id="submit" value="Sign up" /> </td> 
       </tr> 

      </table> 

     </form> 

但很明顯,當用戶沒有正確填寫,它顯示了jQuery框,但形式不復存在。我不會

CTRL + C CTRL + V

我的表格後,每

其他

我相信我沒有做的很好,但不知道這樣做的最佳做法。我對每一個建議都開放(我不在意用別的替換jQuery)

+0

我認爲是因爲你在'IF'語句中嵌套錯誤信息,這是導致問題的原因。如果你把它們放在彼此之外,並且一個接着一個,那麼它將會處理每一個,而不管前一個是真還是假。 – Lee 2015-02-11 10:34:58

+0

但是如果用戶錯誤地點擊了_Submit_,我不想顯示10個框。這不是很友善。我的目標是讓這個頁面變得柔和和用戶友好。有些javascript可能會幫助我在那裏顯示一些東西,當這個領域沒有目標時,有什麼想法? – Raccoon 2015-02-11 10:43:22

+0

你有沒有嘗試jQuery驗證? http://jqueryvalidation.org/ – Giorgio 2015-02-11 10:47:26

回答

1

有幾個答案。我會做的是將錯誤消息存儲在數組中,並將字段名稱作爲關鍵字。然後,您可以顯示該字段下的每條錯誤消息(如以下示例所示),或者選擇在第一個大錯誤<div>中顯示所有內容,然後在字段上添加一些錯誤class(例如,以便它們將以紅色顯示)。

你有一個數組,每個字段有一個錯誤,並帶有它的消息,這是非常方便的,你可以根據需要使用它。

<?php 

    $errorMessages = array(); 
    if (isset($_POST['submit'])) { 
     if (empty($_POST['email']) { 
      $errorMessages['email'] = 'Email field is empty'; 
     } 
     if (empty($_POST['password'] || $_POST['password'] != $_POST['confirm_password']) { 
      $errorMessages['password'] = 'The passwords you entered do not match'; 
     } 
     if (...other error...) { 
      $errorMessages[{fieldName}] = '{message}'; 
     } 

     if (empty($errorMessages)) { 
      // No error : do what you have to do with your datas 
     } 

?> 

<form method="post" action="submenu_signup.php"> 
    <table class="center"> 
     <tr> 
      <td width="50%" ><label for="email">Email :</label></td> 
      <td width="50%" > 
       <input class="more_width" type="text" name="email" id="email" placeholder="Ex : [email protected]" maxlength="45" /> 
       <?php if (!empty($errorMessages['email'])) echo $errorMessages['email']; ?> 
      </td> 
     </tr> 
     <tr> 
      <td width="50%" ><label for="password">Password :</label></td> 
      <td width="50%" > 
       <input class="more_width" type="password" name="password" id="password" maxlength="40" /> 
       <?php if (!empty($errorMessages['password'])) echo $errorMessages['password']; ?> 
      </td> 
     </tr> 
     <tr> ... other fields ... </tr> 
    </table> 
</form> 

(注:。我不知道使用HTML tables這裏是一個很好的做法使用table爲表使用divp和其他相應的HTML標記時,它不是一個table

你也可以在使用PHP提交之前使用Javascript驗證。它可以更方便用戶使用,但如果用戶關閉Javascript(如果想「破解」表單,非常容易使用它),您總是必須進行PHP驗證。