2014-01-27 6 views
0

我正在爲我的html值進行PHP驗證。但是,當PHP驗證 失敗並且我返回頁面時,表單數據將被清除。反正用PHP做什麼保存和PHP重新加載表格數據php - 驗證後爲什麼我的表單數據得到清除

驗證碼

<?php 
    $firstname=$lastname=$contactno=""; 
    $firstnameErr=$lastnameErr=$contactnoErr=""; 

    if ($_SERVER['REQUEST_METHOD']== "POST") { 
     $valid = true; 

    /*FirstName Validation starts here*/  
    if(empty($_POST["fname"])) 
    { 
     $firstnameErr="*firstname is Required"; 
      $valid=false; 
    } 
    else 
    { 
    $firstname=test_input($_POST["fname"]); 
    if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) 
      { 
      $firstnameErr = "&nbsp;&nbsp;Only letters and white space allowed"; 
      $valid=false; 
      }  
    } 
    /*FirstName Validation Ends here*/ 

     /*lastName Validation starts here*/ 
     if(empty($_POST["lname"])) 
     { 
      $lastnameErr="*lastname is required"; 
      $valid=false; 
     } 
     else 
     { 
      $lastname=test_input($_POST["lname"]); 
      if(!preg_match("/^[a-zA-Z ]*$/",$lastname)) 
      { 
       $lastnameErr = "&nbsp;&nbsp;Only letters and white space allowed"; 
       $valid=false; 
      } 
     } 
     /*LastName Validation Ends here*/  

    /*Contact No Validation starts here*/  
     if(empty($_POST["contact"])) 
     { 
      $contactnoErr="*Contact No. is required"; 
      $valid=false; 
     } 
     else 
     { 
      $contactno=test_input($_POST["contact"]); 
      if(!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$contactno)) 
      { 
      $contactnoErr="*Enter a valid contact no"; 
      $valid=false; 
      }  
     } 
     /*Contact No Validation Ends here*/ 

    //if valid then redirect 
     if($valid){ 
      include 'database.php'; 
      echo '<META HTTP-EQUIV="Refresh" Content="0; URL=success.php">';  
     exit; 
     } 
    } 

    function test_input($data) 
    { 
     $data = trim($data); 
     $data = stripslashes($data); 
     $data = htmlspecialchars($data); 
     return $data; 
    } 

    ?> 

形式

<form method="post" action="<?php htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
     <label>Firstname<span class="error">*</span>:</label> 
     <input type="text" name="fname"/> 
     <span class="error"><?php echo $firstnameErr?></span><br /> 
     <br /> 
     <label>lastname<span class="error">*</span>:</label> 
     <input type="text" name="lname"/> 
     <span class="error"><?php echo $lastnameErr?></span><br /> 
     <br /> 
     <label>contactno<span class="error">*</span>:</label> 
     <input type="text" name="contact"/> 
     <span class="error"><?php echo $contactnoErr?></span> 
     <br /> 
    <input style="margin:20px 20px 20px 250px;" type="submit" name="Submit" value="Submit"/> 
</form> 
+0

'' - 您的輸入字段在加載頁面時總是空的。 –

回答

0

當您提交表單時,基本上會重新從頭開始重新加載頁面。而且因爲你不提供它的價值 - 它是空的。你需要在你的表單中做這樣的事情。

<form method="post" action="<?php htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
    <label>Firstname<span class="error">*</span>:</label> 
    <input type="text" value="<?php echo htmlentities($_POST['fname']); ?>" name="fname"/> 
    <span class="error"><?php echo $firstnameErr?></span><br /> 
    <br /> 
    <label>lastname<span class="error">*</span>:</label> 
    <input type="text" value="<?php echo htmlentities($_POST['lname']); ?>" name="lname"/> 
    <span class="error"><?php echo $lastnameErr?></span><br /> 
    <br /> 
    <label>contactno<span class="error">*</span>:</label> 
    <input type="text" value="<?php echo htmlentities($_POST['contact']); ?>" name="contact"/> 
    <span class="error"><?php echo $contactnoErr?></span> 
    <br /> 
    <input style="margin:20px 20px 20px 250px;" type="submit" name="Submit" value="Submit"/> 
</form> 
+1

不要將未過濾的用戶輸入添加到輸出! – Seb

+0

這是一個例子,不是我實際使用的代碼,但我明白了你的觀點並作了編輯,謝謝。 Upvote也爲你。 – Eternal1

+0

@АндрейПочекуев非常感謝你:這個值屬性是沒有驗證的。它的工作。 – Geetha

1

您正在加載的新形式(即使是相同的最後一個加載),並且您沒有任何輸入的value屬性。

0

價值元素添加到您的輸入標籤:

<input type="text" name="fname" value="<?php echo $firstname ?>" />

並確保,所有的價值觀有一個預設。

+0

謝謝你,你的回答是正確的,但是你犯的小錯誤 – Geetha

+0

」/> – Geetha

+0

seb你忘了放$符號 – Geetha

0

從內存中進行驗證,而不是從字段中進行驗證。將它保存到一個對象中,或者只保存在一些你將要使用的變量中。如果這些值無效,只需返回相同的頁面,並再次從變量/對象中填充以允許用戶繼續。

此外,您可能希望在此之前添加客戶端驗證,以便在不向服務器發出請求的情況下修復最瑣碎的無效數據。

相關問題