2014-02-07 72 views
1

我想在按下提交驗證我用PHP的形式,我想以下幾點:驗證形式PHP +不刷新,如果錯誤出現

用戶按下提交,如果沒有發現錯誤,登記表格被處理並且數據被髮送到數據庫。

如果存在錯誤,頁面不會刷新並顯示錯誤控制檯。

每次用戶點擊提交,如果發現錯誤,則會刪除舊郵件&發佈在錯誤控制檯div中的新郵件。

我寫了所有的表單驗證代碼(php),錯誤控制檯div & CSS。我只是不知道如何將它們整合在一起

類似:

if (($username && $email) != 0) { error console + remain on same page } else {send to db}; 

img http://1.ii.gl/5lasBG.png

PHP驗證碼

 $username = 0; 

     $uservar = $_POST['username']; 

     if (empty($uservar)) 
    { 
     $username = 1; 
    } 
else if (!preg_match("/^\w{5,20}$/",$uservar)) 
    { 
     $username = 2; 
    } 


    if (($username) != 0) { 

    echo "<h4 class='error2'><img src='http://s28.postimg.org/ql0x06555/warning6.png' alt='Error'> Error Console</h4> </br>"; 


switch ($username) { 

    case 1: 
     echo "<img src='http://s27.postimg.org/vjxntq073/sign5.png' alt='Error'>"; 
     echo "  The Field 'Username' cannot be left blank"; 
     break; 
    case 2: 
     echo "<img src='http://s27.postimg.org/vjxntq073/sign5.png' alt='Error'>"; 
     echo "  Invalid 'Username' Format - Please use Letters & Numbers only (5-20 Characters)"; 
     break;  
}} 

</div> 

FORM:

<form id="registration-form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 


<input type="text" name="username" id="username" placeholder="Username"> 

<button type="submit" name="submit" id="submit" class="button-submit">Submit</button> 


</form> 
+0

那麼你的代碼的當前行爲是什麼? – ltalhouarne

+0

我編寫了表單,驗證(php),錯誤控制檯div和CSS的所有代碼。我只是不知道如何將它們集成在一起 –

+0

好吧,所以你需要把你的php代碼放在一個合適的php標籤中。然後在你的表單的動作中,你需要調用你的php文件。目前你只是迴應一些action =「<?php echo $ _SERVER ['PHP_SELF'];?>」 – ltalhouarne

回答

1
  1. 從HTML

  2. PHP分割你的主PHP在HTML只能處理簡單的邏輯和顯示結果

作爲一個快速的答案請檢查以下代碼:

<?php 

function valiation(){ 
    $aryMsg = array(); 
    $aryExport = array(); 
    if(!isset($_POST['_submit'])){ 
     //no submit 
     return array($aryMsg, $aryExport); 
    } 

    $aryPostFieldWhiteList = array('username'); 
    //1. post content filter 
    //make sure pass all the field you need from $_POST to $aryExport 
    foreach($aryPostFieldWhiteList as $strField){ 
     $aryExport[$strField] = htmlentities($_POST[$strField], ENT_QUOTES); 
    } 

    //2. post content filter 
    //... 
    //if username is wrong fill $aryMsg['username'] ... etc 

    //3. check validation result 
    if(sizeof($aryMsg)){ 
     //means not pass the validation, return error message and filtered data 
     return array($aryMsg, $aryExport); 
    } 

    //4. past validation 

    //4.1 all db, to insert result 
    //db insert ...etc 


    //4.2 page redirect to thank you page 
    header('Location: /thankyou.php'); 

    die(); 
} 

list($aryMsg, $aryPost) = valiation(); 
?> 
<html> 
    <head> 
    ... 
    </head> 
    <body> 
    <?php if(is_array($aryMsg) && sizeof($aryMsg)):?> 
     <h4 class='error2'><img src='http://s28.postimg.org/ql0x06555/warning6.png' alt='Error'> Error Console</h4> 
     <ul class="error_msg"> 
     <?php foreach($aryMsg as $strMsg):?> 
      <li><?php echo $strMsg;?></li> 
     <?php endforeach;?> 
     </ul> 
    <?php endif;?> 

    <form id="registration-form" action="" method="post"> 

     <input type="text" name="username" id="username" placeholder="Username" value="<?php echo $aryPost['username'];?>"> 

     <button type="submit" name="_submit" id="submit" class="button-submit">Submit</button> 

    </form> 

    </body> 
</html> 
+0

謝謝 - 它是一個很好的代碼 - 瀏覽器仍然刷新壽 –