当点击提交按钮时重定向到其他页面
问题描述:
我试着做一个登录表单。当点击提交按钮时重定向到其他页面
我想在用户点击提交按钮后在同一页上显示错误消息。
如果用户使用正确的信息登录系统,它应该转到索引页面。
这是我写的代码,但它不起作用 - 它每次都会进入相同的页面。
<?php
if (isset($_POST['login'])) {
$uname = $_POST['uname'];
$pass = $_POST['pass'];
$query = mysql_query("select *from user where username='$uname'") or die(mysql_error());
$numrows = mysql_num_rows($query);
if ($numrows != 0) {
while ($row = mysql_fetch_assoc($query)) {
$dbuname = $row['username'];
$dbpassword = $row['password'];
$status = $row['status'];
}
if ($status == 0) {
?>
<div class="alert alert-danger" style="text-align: center ; font-size: 17px;" role="alert">Your account is not yet activated.Please check your email to activate account.</div>
<?php
} else if ($uname == $dbuname && $pass == $dbpassword) {
$_SESSION['username'] = $uname;
header('Location: index.php');
?>
<?php
} else {
?>
<div class="alert alert-danger" style="text-align: center ; font-size: 17px;" role="alert">Incorrect Password.</div>
<?php
}
} else {
?>
<div class="alert alert-danger" style="text-align: center ; font-size: 17px;" role="alert">Incorrect Username.</div>
<?php
}
}
?>
<form name="add" action="login-jobseeker.php" method="post" enctype="multipart/form-data" >
<table class="table ">
<tr>
<td>Username</td>
<td>
<input type="text" required name="uname" class="form-control col-lg-12 " placeholder="Title">
</td>
</tr>
<tr>
<td>Password</td>
<td>
<input type="password" required name="pass" class="form-control col-lg-12 " placeholder="Name">
</td>
</tr>
<tr>
<td></td>
<td>
<a href="register.php?type=<?php echo $type ?>" >Register for a free account</a>
</td>
</tr>
<tr>
<td></td>
<td>
<a href="forget.php" >Forget Password??</a>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="login" class="btn btn-primary" value="Login">
</td>
</tr>
</table>
</form>
如何解决此问题?
答
我想你是在你的表单动作中重定向到这个页面。
<form name="add" action="login-jobseeker.php" method="post" enctype="multipart/form-data" >
把页面放在你想要去的行动。
答
删除操作价值
<form name="add" action="" method="post" enctype="multipart/form-data" >
+0
感谢您的答复。我试试这个。我得到以下错误信息“警告:不能修改标题信息”,因为我已经发送标题时,用户从system.so注销有没有其他方法,我可以使用? – user5008468
答
你可以试试,你触发JavaScript函数下面的方法,当你点击提交按钮。
<input type="submit" name="login" class="btn btn-primary" value="Login" onclick="validate_submit()">
function validate_submit(){
if(INFORMATION_CORRECT){
// perform login and redirection
window.location.href = YOUR_POSTLOGIN_URL;
}else{
// show error either using alert() function or display with div
return false;
}
}
如果浏览器禁用了JavaScript,此方法无效。
感谢您的回复。是的,我想重定向到同一页面,如果有任何error.error应显示在同一页面上。但如果登录成功,它应该重定向到索引页 – user5008468
尝试添加此代码的末尾: header('Location :nextpage.php'); – user3106974