Return to a form called with <a href login.php class="signin">Sign in</a>
I am calling a login form using jquery with.
<div class="session">
<?php if($current_user): ?>
&l开发者_如何学运维t;span>Welcome <?php echo $current_user['first_name']; ?></span>
<li><a href="logout.php" class="signout">Log Out</a></li>
<?php else: ?>
<span>Please Login or Register</span>
<li><a href="login.php" class="signin">Sign In</a></li>
<?php endif; ?>
</div>
Javascript
$(".signin").click(function(e) {
e.preventDefault();
$("fieldset#signin_menu").toggle();
$(".signin").toggleClass("menu-open");
});
$("fieldset#signin_menu").mouseup(function() {
return false
});
$(document).mouseup(function(e) {
if($(e.target).parent("a.signin").length==0) {
$(".signin").removeClass("menu-open");
$("fieldset#signin_menu").hide();
}
});
which works ok, however I want to return to the form if the login does not authenticate.
my PHP would normally be
$user_id = credentials_valid($_POST['username'], $_POST['password']);
if($user_id){
log_in($user_id);
if($_SESSION['redirect_to']){
header("Location: " . $_SESSION['redirect_to']);
unset($_SESSION['redirect_to']);
}else{
header("Location: index.php");
}
}else{
header("Location: login.php?error=1");
exit("You are being redirected");
}
What do I use instead of header("Location: login.php?error=1")
which is just loading a blank page as it not using the .signin cLass.
You could either append a GET variable to the page that the user is redirected to and then check on document load with jquery if the parameter is set, and if so show the form (you already have error=1 appended so you could check for that).
Alternatively a better (in my opinion) solution is to use AJAX. You would submit the form using jQuery's $.ajax() call and check the response that the AJAX call returns with the success
parameter. You would create an action that the AJAX call would POST to and return something like 1 for successful login, and 0 for a failed login.
Does this make sense?
精彩评论