这是我遇到的问题。 我有一个loginform.php,当用户点击登录按钮时,如果用户不存在,我想在我的'#diverror'div上的同一'loginform.php'上显示错误消息。
loginform.php
<form method="post" action="login.php">
<div style='width:500px;margin:auto;border:2px solid darkgrey;margin-top: 50px;'>
<table id='logintable'>
<tr>
<td colspan="2" style='text-align: center;font-weight: bold;'>Login</td>
</tr>
<tr>
<td>Username</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td></td>
<td><button type= submit>Login</button></td>
</tr>
</table>
<div id="diverror"> </div>
</div>
</form>
的login.php
<?php
require 'connect.php';
$username= $_POST['username'];
$password= $_POST['password'];
$sqlcommand = "SELECT * FROM user WHERE username = '$username'";
$result = mysql_query($sqlcommand,$db);
include 'loginform.php';
if(mysql_num_rows($result)>=1)
{
$row = mysql_fetch_assoc($result);
$dbpass = $row['password'];
$dbuser = $row['username'];
$dbactive = $row['active'];
$_SESSION['username']=$dbuser;
header ('Location: index.php');
}
else // If the user dose not exist,
{
//Display this message on the loginform.php > #diverror
}
?>
答案 0 :(得分:2)
在SESSION
...
else // If the user dose not exist,
{
$_SESSION['msg']="User does not exist";
header("Location: loginform.php");
exit();
}
然后只需回显SESSION变量,在显示消息后需要unset
会话变量。
<div id="diverror">
<?php echo $_SESSION['msg'];
unset($_SESSION['msg']); ?>
</div>
注意:必须在两个文件的开头启动会话..
session_start();
答案 1 :(得分:0)
即使用户尚未登录,也要避免显示,请使用
<div class="text-error ">
<?php
session_start();
if (isset($_SESSION['msg']))
{
echo $_SESSION['msg'];
}
unset($_SESSION['msg']); ?>
</div>
这样,只有在设置了msg时才会显示错误,这只能在用户提交时发生。