2

The Problem

I can't seem to get the javascript to redirect after the php has echoed "Success".

Im basically just trying to make it where the javascript post my form data and then the usesr is redirected to their account page. I did have this being done via php, but I wanted the luxury of being able to stay on the same page while logging in.

The Javascript

 <script>
$(document).ready(function() {
//When the form with id="myform" is submitted...
$("#login_form").submit(function() {
     //Send the serialized data to formProcessor.php.
     $.post("login.php", $("#login_form").serialize(),
     //Take our repsonse, and replace whatever is in the "formResponse"
     //div with it.
    function(data) {
          if(data == "Success"){
          location.href="home";
      }
     }
);
return false;
});
});
</script>
Community
  • 1
  • 1
Leighton
  • 103
  • 2
  • 12
  • 2
    Everytime I see ajax for a form submit, and then a javascript redirect, it makes my eyes hurt. – adeneo Jul 24 '13 at 17:45
  • @adeneo what would you do? – Leighton Jul 24 '13 at 17:46
  • Why not just.... submit the form to the act page and let the php redirect? One less set of headers will be sent. One less request will be made. – Kevin B Jul 24 '13 at 17:46
  • 3
    ^^^ What a strange concept, forms can be submitted ? – adeneo Jul 24 '13 at 17:47
  • @adeneo it's justified in this case since it's an ajax call.... header redirect wouldn't do anything. But in general I agree – Orangepill Jul 24 '13 at 17:47
  • @KevinB I didn't want to have to leave the page. – Leighton Jul 24 '13 at 17:48
  • 1
    why echo back `Success`, why not just have php redirect on success, or echo back whatever error message you want when failed? By using `echo('success')`, a clever "**user**" could easily emulate it in console and get redirected without ever actually logging in! – SpYk3HH Jul 24 '13 at 17:48
  • @KevinB And plus, the php wont redirect.. – Leighton Jul 24 '13 at 17:48
  • @Orangepill - it sort of defeats the purpose of an ajax call when you redirect anyway, so it's in no way justified, the default action of a form is to do exactly what the code above does, without any javascript at all ? – adeneo Jul 24 '13 at 17:49
  • @adeneo I didn't want the user to have to wait at the white screen while they were logged in. – Leighton Jul 24 '13 at 17:50
  • @SpYk3HH I'm not stupid, I know how to make it redirect. It just won't do it. It loads the page, it just doesn't redirect.. – Leighton Jul 24 '13 at 17:51
  • And @SpYk3HH If that "clever user" did that, his session would be wrong and he'd be redirected back into the index. – Leighton Jul 24 '13 at 17:52
  • 1
    [how-to-make-a-redirect-in-php](http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php) *|* [http-redirect](http://php.net/manual/en/function.http-redirect.php) *|* [phpredirection](http://php.about.com/od/learnphp/ht/phpredirection.htm) *|* [Codeigniter:redirect](http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html) *|* [php-page-redirect](http://stackoverflow.com/questions/2112373/php-page-redirect) *|* [php-redirect](http://www.cyberciti.biz/faq/php-redirect/) *|* [Google](http://bit.ly/11e4uVi) – SpYk3HH Jul 24 '13 at 17:55
  • 1
    FYI, PHP_Session and Cookies can be [*faked*](http://stackoverflow.com/questions/5905646/faking-session-cookies) as well. Headers too. Using ajax to redirect based on "success" is just bad, I understand using to check for "failed login attempts", but you're better off handling all redirecting server-side. Not trying to argue, just some "constructive criticism" – SpYk3HH Jul 24 '13 at 17:58
  • FYI, Redirecting with JS vs PHP does not create any extra security issues. Authentification needs to be checked on each page load no matter where the user came from. The only difference is that with AJAX you can show error messages without reloading and rerendering a whole page. – Clarence Jul 24 '13 at 18:22

2 Answers2

3

You redirect using document.location or window.location not location.href

window.location = "home.html";

// or 

document.location="home.html"; //Will not work on older IE Version

Note:

document.location was originally a read-only property which Gecko & Webkit browsers allowed to edit. For cross browser compatibility use window.location as it a read/write property.

Starx
  • 77,474
  • 47
  • 185
  • 261
2

You can try any one of this

Method 1:

window.location.replace("home.html");

Method 2:

window.location.href = "home.html";

Method 3:

$(location).attr('href',"home.html");

Hope this will help you.