0

Hi there!

I'm using PHP together with jQuery/AJAX to submit a login form without reloading the page. Basically the code works like this:

Login.php (The actual form) -> process-login-admin.php (server-side validation and authentication) -> Echo error message
The error messages is displayed in a hidden div (#feedback) on "Login.php" that i set to visible once "login-process.php" echos something.

This is how i do it:

$.post('process-login-admin.php', $("#loginprocess").serialize(), function(data) {
                                                     $("#feedback").html(data).show("slow");
                                                     });
                                              }

Everything works just fine except one thing. When a user successfully logs in i would like to do a simple header location to the users dashboard/profile page. However the dashboard loads inside the message div i created instead of changing url.

Anyone that could give me a few pointers on how to solve this?

3
Contributors
4
Replies
8
Views
5 Years
Discussion Span
Last Post by kletig
1

Because you are using AJAX, you will have to handle the redirect in Javascript, not PHP. So instead of doing the header in PHP, notify Javascript with a particular string (or JSON), to do the redirect for you.

0

Thank you pritaeas!
I seem to have solved it by doing this once the user is logged in:

echo '<script>location.href="index.php";</script>' ;

Is there any downside to this approach? Will this work in all browsers?

0

echo '<script>location.href="index.php";</script>' ;

why not just do header('Location: index.php"); if you are echoing it out?

There should be a point in your js where the login succeeds and redirect then:

messageDiv.innerHTML = req.responseText;
if(req.responseText == 'Success'){window.location = 'index.php';}

i always thought it was window.location, since your login is javascript the redirect should 100% work window.location is a very wide standard

or you could just fill in the div contents with the contents of the dashboard if you have that available in ajax

if(req.responseText == 'Success'){
contentDiv.innerHTML = getMyDashboard();
}
0

Thanks Biiim!

Your answer got me in the right direction. I have solved this by implementing your "responsText" method.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.