2015-12-29 22 views
2

我一直在嘗試使用PHP來驗證HTML表單,當然這些表單已成功運行。問題然後我有點問題在哪裏添加包含文件,其中包含一個成功的消息後進行全部驗證。 這裏是我的代碼:在PHP多表單驗證中使用if語句時遇到的困難

<?php 
     $status=$codeErr=""; 
     if (isset($_GET['check'])) { 
      $number=$_GET['number']; 
      $code=$_GET['code']; 

      if($number!=""){ 
       if(preg_match("/[0-9]/",$number) and strlen($number)=="11"){ 
        $four=substr($number, 0,4); 
        if($four!="0706" and $four!="0813" and $four!="0803" and $four!="0806" and $four!="0703" and $four!="0816" and $four!="0810" and $four!="0814" and $four!="0903"){ 
        $status='<p class="status">Number Entered is not an MTN Number</p>'; 
        } 
       } 
       else{ 
        $status='<p class="status">Invalid Number Format</p>'; 
       } 
      } 
      else{ 
        $status='<p class="status">Please Enter an MTN Number</p>'; 
       } 
      if($code!=""){ 
       if($code!=="324"){ 
        $codeErr='<p class="status">Winning Code Not Recognized</p>'; 
       } 
      } 
      else{ 

       $codeErr='<p class="status">Please Enter Your Winning Code</p>'; 

      } 


     } 

這包括文件顯示形式,如果沒有設置。

 else if(!isset($_GET['check'])){ 

      include('includes/check.php'); 

     } 
    ?> 

現在我還有一個包括文件,其中包含如果在支票形式熄滅所有條件都滿足將要顯示的消息。我在哪裏包括它?

+1

我認爲,最好你去通過此鏈接,一旦[確認](http://www.w3schools.com/php/php_form_validation.asp)。這以簡單的方式解釋了很多。不需要混淆。 – JTheDev

回答

0

你應該使用異常:

try { 

    if (!$somethingOne) { 
     throw new Exception('Something wrong one!'); 
    } 
    if (!$somethingTwo) { 
     throw new Exception('Something wrong two!'); 
    } 
    if (!$somethingMore) { 
     throw new Exception('Something wrong more!'); 
    } 

    // success here only 
    echo 'All right!'; 

} catch (Exception $e) { 
    echo $e->getCode() . '<p>' . $e->getMessage() . '</p>'; 
} 
+0

仍然沒有解決它 –