谁能解释我如何在同一页面中检查和注册用户名和密码?
如何回显表单中的"用户名和密码错误"消息?
提前谢谢。
检查登录
$result=mysql_query($sql);
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
形式
<form class="clearfix" name="form1" method="POST" action="">
将
表单操作设置为当前.php文件。在脚本中,检查您是否已经提交了表单(例如,提交按钮的值为 $_POST
)
如果有答案,请继续使用登录代码。如果没有,请创建一个新变量,例如 $errorMessage
。在表单中的某处回显此错误消息。(如果这是用户第一次加载脚本的时间,则此变量中将没有任何内容,因此它将不显示任何内容。
如果您将$errorMessage的回声放在带有一些<div>
或任何您想用于此任务的 if 语句中,那会更优雅。
要显示错误,请创建一个变量$error = "Wrong Username or Password";
然后在表单的底部写:
if(isset($error)) {
echo $error;
}
使用 isset
和 isempty
检查返回的数据。
if(isset($_POST["formvariable"]))
{ }
同样在表单字段中将操作更改为"currentfilename.php"
真的是基础知识
if(isset($_POST["submit"])){
// user have submitted the form , proceed checking username/password and then start session
}else{
// display login form
}
我建议使用Ajax方法。最终结果要干净得多。