PHP validate同时执行IF语句和ELSE语句
正如标题所说,我在验证php方面遇到了问题。这只是一个简单的登录php,如果用户输入了错误的信息,将执行一个验证表单,说明它将在5秒倒计时后返回到上一个登录页面。但是,如果用户输入正确的用户名和密码,它只会在验证php上显示欢迎文本。然而,每当我运行程序时,它都会执行欢迎文本和倒计时,不管输入的信息是对还是错 这是我的第一个php:PHP validate同时执行IF语句和ELSE语句,php,html,validation,Php,Html,Validation,正如标题所说,我在验证php方面遇到了问题。这只是一个简单的登录php,如果用户输入了错误的信息,将执行一个验证表单,说明它将在5秒倒计时后返回到上一个登录页面。但是,如果用户输入正确的用户名和密码,它只会在验证php上显示欢迎文本。然而,每当我运行程序时,它都会执行欢迎文本和倒计时,不管输入的信息是对还是错 这是我的第一个php: <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or
<!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>
身体{
保证金:0;
字体系列:Calibri}
.style1{
颜色:#FFFFFF}
从用户示例1获取输入
用户名
密码
以下是验证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>
无标题文件
var-ctr=5;
函数倒计时(){
window.document.getElementById(“cnt”).innerHTML=ctr;
ctr--;
如果(ctr)
在php验证中,您还没有关闭第二个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>';
}
}
?>
您在else
之前丢失了一个右括号。尝试了它。在验证时仍然得到相同的输出:欢迎“$\u POST[“txtUsername”]。!”;}否则{echo”用户名和密码无效。“echo”这将在2秒内重定向到登录表单…;echo“”;}}?>它对我来说工作正常。你到底出了什么错?你使用的是.html文件还是.php?不管我输入什么,验证仍然会生成以下输出:欢迎“$_POST[“txtUsername”]。!”;}否则{echo”用户名和密码无效。“echo”这将在2秒内重定向到登录表单…“;echo”;}}?>倒计时仍然有效,但整个验证显示的结果与上面的输出一样,原始。要么您没有安装PHP,要么您使用的是.html文件而不是.phpTried文件,但仍然是一样的。我认为代码不是问题……可能是xampp或其他东西造成的/