2012-06-28 22 views
1

我的表單與PHP完成的驗證正常工作。 我有三個字段:名稱,電子郵件和消息。 表單和PHP代碼在同一個pgae中,當用戶提交表單時,會調用相同的頁面進行驗證。表單提交出錯時輸入值丟失

當用戶提交表單時,調用相同的頁面並檢查表單是否被提交。 如果表單已提交,則會對空白條目進行驗證,並在字段下方顯示錯誤消息以通知用戶該字段留空。它還顯示字段旁邊的錯誤圖標。

直到這,它工作正常。

但是,問題是,如果用戶填寫了任何字段,例如名稱歸檔並將其他兩個字段(EMail和Message)留空,則在提交時,會爲空白字段引發錯誤消息,但是對於由用戶填充的名稱字段,它將清空內容並顯示空白名稱字段,並且不顯示錯誤(如前面的用戶填寫過的那樣)。

我唯一擔心的是當它在提交後重新運行表單時,它還應該在提交之前重新加載用戶輸入的相應字段中的較早值。

以下是PHP驗證碼。

<?php 
error_reporting(E_ALL & ~E_NOTICE); 

    if(isset($_POST['nameField_Name']) AND isset($_POST['nameField_EMail']) AND isset($_POST['nameField_Message']) AND isset($_POST['nameSubmit'])){ 
     // Form Submited 
     if ($_POST['nameField_Name']) { 
    $phpVarNameField = mysql_escape_string($_POST['nameField_Name']); 
} else { 
    $errormsgNameField = "Name field is required, Please enter your Name."; 

} 

if ($_POST['nameField_EMail']) { 
    $phpVarEMailField = mysql_escape_string($_POST['nameField_EMail']); 
} else { 
    $errormsgEMailField = "E-Mail field is required, Please enter your E-Mail ID."; 
} 

if ($_POST['nameField_Message']) { 
    $phpVarMessageField = mysql_escape_string($_POST['nameField_Message']); 
} else { 
    $errormsgMessageField = "Message field is required, Please enter your Message."; 
} 
    } 
?> 

以下是表格代碼。

   <form name="myform" action="contactus.php" method="post""> 
       <div id="r1"> 
        <div id="r1c1"> 
         <input type="text" name="nameField_Name" id="idField_Name" placeholder="Enter your name here"/> 
        </div> 
        <div id="r1c2"> 
       <?php 
         if(isset($errormsgNameField)){ // Check if $msg is not empty 
        echo '<img src="error.png" width="45" height="45" style="margin: 5px 0px" alt="">'; 
         } 
        ?> 
        </div> 
       </div> 
       <div id="afterr1"> 
       <?php 
         if(isset($errormsgNameField)){ // Check if $msg is not empty 
        echo '<div class="statusmsg" id="idErrorMsgNameField">'.$errormsgNameField.'</div>'; // Display our message and wrap it with a div with the class "statusmsg". 
         } 
        ?> 

       </div> 
       <div id="r2"> 
        <div id="r2c1"> 
         <input name="nameField_EMail" type="text" id="idField_EMail" placeholder="Enter your E-Mail address here" /> 
        </div> 
        <div id="r2c2"> 
       <?php 
         if(isset($errormsgEMailField)){ // Check if $msg is not empty 
        echo '<img src="error.png" width="45" height="45" style="margin: 5px 0px" alt="">'; 
         } 
        ?> 
        </div> 
       </div> 
       <div id="afterr2"> 
       <?php 
         if(isset($errormsgEMailField)){ // Check if $msg is not empty 
        echo '<div class="statusmsg" id="idErrorMsgEMailField">'.$errormsgEMailField.'</div>'; // Display our message and wrap it with a div with the class "statusmsg". 
         } 
        ?> 
       </div> 
       <div id="r3"> 
        <div id="r3c1"> 
         <textarea name="nameField_Message" id="idField_Message" placeholder="Enter your message for us here"></textarea> 
        </div> 
        <div id="r3c2"> 
       <?php 
         if(isset($errormsgMessageField)){ // Check if $msg is not empty 
        echo '<img src="error.png" width="45" height="45" style="margin: 115px 0px" alt="">'; 
         } 
        ?> 

        </div> 
       </div> 
       <div id="afterr3"> 
       <?php 
         if(isset($errormsgMessageField)){ // Check if $msg is not empty 
        echo '<div class="statusmsg" id="idErrorMsgMessageField">'.$errormsgMessageField.'</div>'; // Display our message and wrap it with a div with the class "statusmsg". 
         } 
        ?> 
       </div> 

       <div id="r4"> 
        <div id="r4c"> 
         <input type="Submit" name="nameSubmit" id="idButton_Submit" value="Submit" alt="Submit Button"/> 
        </div> 
       </div> 
       </form> 

任何幫助將是偉大的。

謝謝。

回答

1

試着改變你的標籤,如:

<input type="text" 
    name="nameField_Name" 
    id="idField_Name" 
    placeholder="Enter your name here" 
    value ="<?php 
      if (isset($phpVarNameField)) 
       echo $phpVarNameField; 
     ?>" 
/> 
....... 
<input 
    name="nameField_EMail" 
    type="text" 
    id="idField_EMail" 
    placeholder="Enter your E-Mail address here" 
    value ="<?php if (isset($phpVarEMailField)) echo $phpVarEMailField; ?>" 
/> 
....... 
<textarea name="nameField_Message" id="idField_Message" placeholder="Enter your message for us  
here" value ="<?php if (isset($phpVarMessageField)) echo $phpVarMessageField; ?>" ></textarea> 

祝您好運!

+0

一切工作正常,舊的條目檢索輸入字段。但是文本區域的舊條目不會被放回。儘管我在表單提交後測試了textarea變量($ phpVarMessageField)中的舊條目,但在表單提交後沒有將其返回到textarea中,任何Name或E Mail字段都留空。任何想法可能是什麼原因? – user1478475

+0

哦對不起!這是我的錯誤。 EscalinNancy

4

您需要在您的<input>元素添加value屬性:

<input type="text" 
     name="whatever" 
     value="<?php echo htmlspecialchars($_POST['whatever']); ?>" 
> 

這可能是更容易閱讀,如果PHP輸出領域:

<?php 
printf('<input type="text" name="%s" value="%s">', 
     'whatever', 
     htmlspecialchars($_POST['whatever'])); 
?> 

這甚至可以被包裹在一個功能,因此您無需爲每個表單字段重新輸入它。請撥打htmlspecialchars。這是需要的,以便<>和引號不會破壞您的HTML文檔。

1

那麼,你可以使用jQuery驗證插件進行驗證 - 很簡單,很好。 jQuery plugin

或者用PHP存儲數組中的POST數據,檢查錯誤以及非空字段設置爲輸入文本的值。

if (isset($_POST)) { 
    $data = $_POST; 
} 
foreach ($data as $row) { 
    if ($row == "") 
     $error = true; // do what ever you want 
} 

and then in form 
<input type="text" name="name" value="<?php ($data['name'] != "")? $data['name'] : '' ?>" /> 

這樣的事情。