Discuss AJAX - redirect after login is validated in the JavaScript Development forum on Dev Shed. AJAX - redirect after login is validated JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
The ASP Free website provides in-depth information on the latest developer tools available from Microsoft. Our cadre of writers, highly experienced industry experts, reveals the best ways to use established technologies as well as new and emerging technologies. Our coverage of Microsoft's development and administration technologies is among the most respected in the IT industry today.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Intel® Graphics Performance Analyzers is a powerful tool suite for analyzing and optimizing your games, media, and graphics-intensive applications. Used by some of the best developers on the planet, Intel GPA lets you maximize your app’s performance.
Posts: 70
Time spent in forums: 13 h 51 m 14 sec
Reputation Power: 6
AJAX - redirect after login is validated
I am validating login information via AJAX and that works fine. However, I want to redirect the page to the "secure dashboard" once the user has entered in valid username/password information. How would you go about doing this?
I tried sending headers back via php, but as I have read in other forums, it only redirects that particular <DIV> to the redirection URL.
I do not believe that I need any code up here because it is kind of generic in nature.
Posts: 70
Time spent in forums: 13 h 51 m 14 sec
Reputation Power: 6
Quote:
Originally Posted by huyaroo
Use window.location='newURL.html'; once your javascript returns a success message.
I tried that, but doesn't work
Right now I have it returning "You have logged in successful" just to make sure that it did indeed work. So, then I tried returning "<script type='text/javascript'>window.location='newURL'</script>", but that didn't work
Posts: 1,520
Time spent in forums: 2 Weeks 6 Days 2 h 36 m 42 sec
Reputation Power: 438
You don't print that out using innerHTML or whatever. Can you post a snippet of your code? Right around where you are printing the "You have logged in successful" message.
Here is the ajax function that is called when the form is submitted:
Code:
function login() {
var user = document.getElementById('agentUsername').value;
var pass = document.getElementById('agentPassword').value;
var remember;
if(document.agentLogin.rememberMe.checked) {
remember = "y";
}
else {
remember = "n";
}
new Ajax.Updater('errors', 'check.php?login=true&auser='+user+'&apass='+pass+'&remember='+remember);
new Effect.Highlight('errors', {startcolor:'#ff000',endcolor:'#ff99ff', restorecolor:'#ff99ff'});
}
Here is the PHP function that is run to log the user in and return the text:
PHP Code:
function login($u,$p,$remember) {
if($u == "" or $p == "") {
echo "Please enter a username and a password";
}
else {
dbConnect();
// Queries the database
$sql = "
SELECT bg_username,bg_password
FROM agents
WHERE bg_username = '$u'
AND bg_password = '$p'
";
$res = mysql_query($sql) or die(mysql_error());
$num = mysql_num_rows($res);
Posts: 1,520
Time spent in forums: 2 Weeks 6 Days 2 h 36 m 42 sec
Reputation Power: 438
Are you using prototype? It seems like you need to add a response handler for your AJAX call so that you can differentiate between a successful login and one with errors. You can do that by adding another method using the onComplete option.
Your new method would look something like this:
Code:
function checkLogin(response)
{
if (response == "Success!") {
window.location='newurl.html';
} else {
alert("You have something wrong in your form.");
}
}
example of an AJAX call with onComplete:
Code:
var ajax = new Ajax.Updater(
'datestr', // DIV id (XXX: doesnt work?)
'date.cgi', // URL
{ // options
method:'get',
onComplete: checkLogin
});
Posts: 2
Time spent in forums: 7 m 54 sec
Reputation Power: 0
Quote:
Originally Posted by huyaroo
Code:
function checkLogin(response)
{
if (response == "Success!") {
window.location='newurl.html';
} else {
alert("You have something wrong in your form.");
}
}
Hi. I was also thinking about this solution. But I think this method is vulnerable to hackers. Anyone who know js can just call checkLogin('success'); and viola! logged in. Is there a better way to handle safe login? Thanks!
Posts: 1,021
Time spent in forums: 1 Month 1 Week 6 Days 14 h 33 m 57 sec
Reputation Power: 1299
You did not get the point , js is only used for redirection in this case, login process is dependent on the server side script so wont affect much, calling checkLogin('success'); will redirect em but not logged in.
Quote:
Originally Posted by oliever
Hi. I was also thinking about this solution. But I think this method is vulnerable to hackers. Anyone who know js can just call checkLogin('success'); and viola! logged in. Is there a better way to handle safe login? Thanks!
Posts: 2
Time spent in forums: 7 m 54 sec
Reputation Power: 0
Quote:
Originally Posted by sarav_dude
You did not get the point , js is only used for redirection in this case, login process is dependent on the server side script so wont affect much, calling checkLogin('success'); will redirect em but not logged in.