Forum Moderators: open

Message Too Old, No Replies

Jquery/Ajax in Popup Window - Close Window after User Sucessful login

         

tec4

8:39 pm on Nov 15, 2011 (gmt 0)

10+ Year Member



I've got a simple Jquery Ajax request working that logs in a user and returns a simple "Success" or "Failure" text back to the current page in a specific div.

I would like, however, to make it so that if the user has succesfully logged in - then the pop-up window would close but if they did not pass the JS validation or the PHP validation, then they would stay in the pop-up window and the errors would be shown.

Current Jquery code:

$(document).ready(function(){

$("#myform").validate({

debug: false,
rules: {
pass: "required",
user: {
required: true,
email: true
}
},

messages: {

pass: "Please provide your password.",
user: "Not a valid email addesss to log you in.",
},

submitHandler: function(form) {

$.post('/register/process.php', $("#myform").serialize(), function(data) {

$('#results').html(data);

});
}

});
});

Have tried using the window.close(); but that obviously closes the window no matter what the result of the login is.

*Any way to check if it has actually succeeded or if if the user input is not correct?

Thanks!

-Tec

enigma1

9:42 pm on Nov 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes there are ways to check if the login is successful but you need to return an object back from the php script or an array that will have the message to print perhaps, the status of the attempt etc. (if you want to do it properly)

You cannot do just echo php objects but you can output strings. So you need to serialize the results from the php script in some way and send them back to the client. Some people use json for this I prefer to use just serialization more compatible.

So if your php result is an array
$result = array(
'status' => $login_status,
'message' => $message,
);

$output = serialize($result);
echo $output;

Then you need to reverse the results, there are jquery serialize/unserialize plugins you could use.

[code.google.com...]

Then you convert the results into a js array or object and you have pretty much everything to process on the client's end.

Or you can use json which does a similar thing, or you can do the quick and dirty way where you could echo just a specific string like 0 or 1, from the php that signifies whether the login was successful. But the code won't be too reusable.

Fotiman

9:52 pm on Nov 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




$.post('/register/process.php', $("#myform").serialize(), function(data) {

$('#results').html(data);
if (data == "Success") {
// Close the window
window.close()
}
});