Php 单击“提交”按钮时重定向到其他页面
我正在试着做一个登录表单 我想在用户单击提交按钮后在同一页面上显示错误消息Php 单击“提交”按钮时重定向到其他页面,php,Php,我正在试着做一个登录表单 我想在用户单击提交按钮后在同一页面上显示错误消息 <input type="submit" name="login" class="btn btn-primary" value="Login" onclick="validate_submit()"> function validate_submit(){ if(INFORMATION_CORRECT){ // perform login and redirection
<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;
}
}
如果用户使用正确的信息登录到系统,则应转到索引页
这是我写的代码,但它不起作用-每次都会转到同一页
<?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" >
您可以尝试以下方法,单击submit按钮时触发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;
}
}
函数validate_submit(){
如果(信息正确){
//执行登录和重定向
window.location.href=您的\u POSTLOGIN\u URL;
}否则{
//使用alert()函数或display with div显示错误
返回false;
}
}
如果浏览器禁用了javascript,此方法将不起作用。谢谢您的回复。是的,如果有任何错误,我希望重定向到同一页。错误应该显示在同一页上。但是如果登录成功,它应该重定向到索引页尝试在php代码的末尾添加此代码:header('Location:nextpage.php');谢谢你的回复。我试试这个。我收到以下错误消息“警告:无法修改标题信息”,因为当用户从系统注销时,我已经发送了标题。那么,还有其他方法可以使用吗?