简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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