我有index.php,在index.php中有一个带会话的登录表单。如果用户名和密码正确,则会转到home.php。但是如果用户名或密码不正确,我想在登录表单上方显示包含类似的错误消息的div。
我希望此警告div被隐藏,只有在用户名或密码不正确时才会显示。我已经尝试了一些其他的解决方案,但它们都没有工作,首先如果我把div放在其他条件下,它将出现在登录表单上方,无论条件是什么。其次,如果我把javascript改为样式display = block,它就不会出现在索引页面中。请帮我解决这个问题。感谢。
的index.php
<?php
session_start();
if(isset($_POST['login']))
{
$user = $_POST['user'];
$pass = $_POST['pass'];
if($user == "admin" && $pass == "admin")
{
$_SESSION['user']=$user;
?>
<script language="javascript">window.location.href='home.php' </script>
<?php
} else {
//What should I do here?
?>
<script language="javascript">document.getElementById("warning").style.display = "block" </script>
<?php
}
}
?>
<!-- Warning that I want to show if username or password is incorrect -->
<div id="warning" class="w3-container w3-red w3-text-white w3-card-2 w3-animate-opacity" style="position: relative;margin-top: 20px;display:none">
<span onclick="this.parentElement.style.display='none'" class="w3-closebtn">×</span>
<strong>Warning!</strong> Incorrect Username or Password!
</div>
<!-- Login form -->
<form class="w3-container" action="" method="post">
</form>
[编辑]
我终于通过遵循Sharky的建议来解决问题,将php代码放在警告div之下。在其他情况下我仍然使用javascript将更改警告div的样式显示阻止。谢谢你的帮助:)
答案 0 :(得分:1)
最好的方法应该是你已经尝试过的方法:在else
案例中使用#warning div。也许它没有用,因为你选错了else
?
<?php
session_start();
if (isset($_POST['login'])) {
$user = $_POST['user'];
$pass = $_POST['pass'];
if($user == "admin" && $pass == "admin") {
$_SESSION['user']=$user;
?>
<script type="text/javascript">window.location.href='home.php';</script>
<?php
} else {
?>
<div id="warning" class="w3-container w3-red w3-text-white w3-card-2 w3-animate-opacity" style="position: relative;margin-top: 20px;">
<span onclick="this.parentElement.style.display='none'" class="w3-closebtn">×</span>
<strong>Warning!</strong> Incorrect Username or Password!
</div>
<?php
}
}
?>
//Login form
<form class="w3-container" action="" method="post">
</form>
另一个tipp(与你的问题无关,但无论如何都可以提供帮助):你可以简单地使用php,而不是使用JavaScript重定向,这样就不再需要额外的浏览器往返:< / p>
header('Location: home.php');
这要求,没有正常的&#39;输出是在header()
调用之前创建的,并且可以在此示例中使用,但如果之前有一些html代码,则可能会失败。
答案 1 :(得分:0)
你的div没有显示的原因是因为你的javascript设置display:block;
正在执行之前,你的标记中会产生div。
简单场景(失败):
<html>
<head>
<script language="javascript">
document.getElementById("warning").style.display = "block";
</script>
</head>
<body>
<div id="warning" style="display:none;">hello</div>
</body>
</html>
&#13;
这将导致
Uncaught TypeError: Cannot read property 'style' of null
因为当下 javascript尝试获取标识为warning
的元素,所以只有此标记&#34;现有&#34;在浏览器中:
<html>
<head>
<script language="javascript">
document.getElementById("warning").style.display = "block";
</script>
您是否在上述标记中看到任何ID为warning
的div?不,根本没有任何div。实际上还没有<body>
!
因此,为了使其正常工作,您的javascript必须在该div之后。把它放在</body>
之前的底部。请参阅以下内容:
<html>
<head>
</head>
<body>
<div id="warning" style="display:none;">hello</div>
<script language="javascript">
document.getElementById("warning").style.display = "block"
</script>
</body>
</html>
&#13;
现在,关于你的代码:如果你不想移动那个if
的大块,你可以设置一个像
$need_to_show_warning = false;
if($user == "admin" && $pass == "admin")
{
$_SESSION['user']=$user;
?>
<script language="javascript">window.location.href='home.php' </script>
<?php
} else {
//What should I do here?
$need_to_show_warning = true;
}
然后,在你的div之后:
<!-- Warning that I want to show if username or password is incorrect -->
<div id="warning" class="w3-container w3-red w3-text-white w3-card-2 w3-animate-opacity" style="position: relative;margin-top: 20px;display:none">
<span onclick="this.parentElement.style.display='none'" class="w3-closebtn">×</span>
<strong>Warning!</strong> Incorrect Username or Password!
</div>
<?php
if($need_to_show_warning)
{
?>
<script language="javascript">document.getElementById("warning").style.display = "block"; </script>
<?php
}
答案 2 :(得分:0)
这个怎么样。
<?php
session_start();
if(isset($_POST['login']))
{
$user = $_POST['user'];
$pass = $_POST['pass'];
if($user == "admin" && $pass == "admin")
{
$_SESSION['user']=$user;
?>
<script language="javascript">window.location.href='home.php' </script>
<?php
} else {
//What should I do here?
?>
<!-- Warning that I want to show if username or password is incorrect -->
<div id="warning" class="w3-container w3-red w3-text-white w3-card-2 w3-animate-opacity" style="position: relative;margin-top: 20px;">
<span onclick="this.parentElement.style.display='none'" class="w3-closebtn">×</span>
<strong>Warning!</strong> Incorrect Username or Password!
</div>
<!-- Login form -->
<form class="w3-container" action="" method="post">
</form>
<?php
}
}
?>
?>