就像標題所說的,我在驗證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>
你錯過了'else'前一個右括號。 – Eraph