简体   繁体   English

单击提交按钮时重定向到其他页面

[英]redirect to other page when click submit button

I'm try to make a login form. 我正在尝试制作一个登录表格。

I want to show error messages on same page after the user has clicked the submit button. 用户单击提交按钮后,我想在同一页面上显示错误消息。

If the user logged to the system with correct information it should go to the index page. 如果用户使用正确的信息登录到系统,则应转到索引页面。

This is the code I have written, but it doesn't work - it is going to the same page every time. 这是我编写的代码,但是不起作用-每次都会转到同一页面。

<?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>  

How do I resolve this issue? 我该如何解决这个问题?

I think you're redirecting to this page in your form-action. 我认为您正在通过表单操作重定向到此页面。

<form name="add" action="login-jobseeker.php" method="post" enctype="multipart/form-data" >

Put the page where you want to go to in the action. 在操作中将页面放到您想去的地方。

删除动作值

<form name="add" action="" method="post" enctype="multipart/form-data" >

You can try the following method where you trigger a javascript function when you click the submit button. 您可以尝试以下方法,在您单击“提交”按钮时触发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;
    }
}

This method will not work if the browser has javascript disabled. 如果浏览器禁用了javascript,则此方法将不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM