我有下面的登录表(login.php),我要求输入用户名和密码。
<form action="processlogin.php" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Login">
</form>
以下是我的processlogin.php文件中的代码片段
if(!$_POST["username"] || !$_POST["password"])
{
$msg = "You left one or more of the required fields.";
echo $msg;
//header("Location:http://localhost/login.php");
}
这段代码检查是否所有必填字段都填满了。如果不是,则显示错误消息。
到现在为止,一切都很好。
我的问题是,错误消息显示在纯白色页面中。我想在login.php文件的登录表单上面显示它。我应该如何更改我的代码来获得我的功能。
发布于 2013-11-01 15:07:35
我更喜欢Jquery验证或基于Ajax的身份验证。但是你仍然可以这样做:
将错误消息放入会话中,如下所示:
$_SESSION['Error'] = "You left one or more of the required fields.";
而不是像这样简单地展示它:
if( isset($_SESSION['Error']) )
{
echo $_SESSION['Error'];
unset($_SESSION['Error']);
}
在这种情况下,您可以在不同的操作中分配多个消息。
发布于 2013-11-01 15:03:26
header("Location:http://localhost/login.php?x=1")
在login.php中
if(isset($_GET('x'))){
//your html for error message
}
发布于 2013-11-01 15:05:17
希望对你有帮助,
在processlogin.php中,
if(!$_POST["username"] || !$_POST["password"])
{
$msg = "You left one or more of the required fields.";
$msgEncoded = base64_encode($msg);
header("location:login.php?msg=".$msgEncoded);
}
在login.php文件中,
$msg = base64_decode($_GET['msg']);
if(isset($_GET['msg'])){
if($msg!=""){
echo $msg;
}
}
https://stackoverflow.com/questions/19722180
复制相似问题