2015-09-30 74 views
1

就像標題所說的,我在驗證php時遇到問題。這只是一個簡單的登錄php,如果用戶輸入了錯誤的信息,驗證表單會執行,表示它將在倒計時五秒鐘後返回到上一個登錄頁面。但是,如果用戶輸入正確的用戶名和密碼,它只會在驗證php中顯示一個歡迎文本。但是,無論輸入的信息是對還是錯,無論何時運行程序,它都會執行歡迎文本和倒計時。PHP validate同時執行IF語句和ELSE語句

這是我的第一個PHP:

<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
    <style> 
     body { 
     margin:0; 
     font-family: Calibri} 

     .style1 { 
     color: #FFFFFF} 

    </style> 
</head> 
<body> 
    <h1>Getting Input from USER</h1><h2>EXAMPLE 1</h2> 
    <form method="post" action="validatea.php"> 
    <table width="200" border="0" cellspacing="0" cellpadding="5"> 
     <tr> 
      <th bgcolor="#006699" scope="row"><span class="style1">Username</span></th> 
      <td><label> 
      <input type="text" name="txtUsername" id="txtUsername" required="required"/> 
      </label></td> 
     </tr> 

     <tr> 
      <th bgcolor="#006699" scope="row"><span class="style1">Password</span></th> 
      <td><label> 
      <input type="password" name="txtPassword" id="txtPassword" required="required"/> 
      </label></td> 
     </tr> 

     <tr> 
      <th colspan="2" scope="row"><div align="right"> 
      <input type="reset" value="Clear"/> 
      <input type="submit" value="Login"/> 
      </div></th> 
     </tr> 
     </table> 
     </form> 
     <hr/> 


</body> 
</html> 

這裏的驗證PHP:

<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>Untitled Document</title> 

    <script> 
     var ctr=5; 
     function countdown(){ 
      window.document.getElementById("cnt").innerHTML=ctr; 
      ctr--; 
      if (ctr<0) window.location="samplea.php"; 
      setTimeout("countdown()", 1000); 
     } 
     </script> 

</head> 

<body> 

<?php 
    if(isset($_POST["txtUsername"])){ 
     $uname="user";$psw="hello"; 
      if($uname==$_POST["txtUsername"] && $psw==$_POST["txtPassword"]){ 
       echo "<h2>Welcome " .$_POST["txtUsername"]."!</h2>"; 
      else{ 
       echo"Username and Password is invalid.<br/><br/>"; 
       echo'This will redirect to login form in <label id="cnt"></label>seconds...'; 
       echo'<script> countdown();</script>'; 
      } 
     } 
    } 
?> 

</body> 
</html> 
+1

你錯過了'else'前一個右括號。 – Eraph

回答

1

您missplaced在第二個} IF

<?php 
    if(isset($_POST["txtUsername"])){ 
     $uname="user";$psw="hello"; 
     if($uname==$_POST["txtUsername"] && $psw==$_POST["txtPassword"]){ 
       echo "<h2>Welcome " .$_POST["txtUsername"]."!</h2>"; 
     }else{ //Forgot to close if } 
       echo"Username and Password is invalid.<br/><br/>"; 
       echo'This will redirect to login form in <label id="cnt"></label>seconds...'; 
       echo'<script> countdown();</script>'; 
     } 
    } 
?> 
+0

試過了。仍然得到相同的輸出驗證: 歡迎「$ _ POST [」txtUsername「]。」!「;}其他{回聲」用戶名和密碼是無效的。 「 」; echo'This將重定向到2秒鐘內的登錄表單...'; echo'';}}?> – Raios

+0

這對我來說工作得很好,你究竟發生了什麼錯誤? –

+0

你使用的是.html文件還是.php? –

0

您還沒有關閉你的第二個如果驗證PHP! 關閉它,它會正常工作

+0

試過了,但還是一樣。我認爲代碼不是問題......也許這是xampp或什麼。 :/ – Raios