0

Edit There is something wrong as the inside of the .done callback does not execute, even a simple alert('something');

I've noted the error pointed out by Sverri I should note that this script is included externally, maybe that's the problem?

The login is successful, if I refresh the same page that I'm on, I am redirected by the PHP session catch at the top page. I'm trying to avoid having the login stuff for PHP on the same page as the form, but rather include it as a separate page.

When I process this, within the .done catch, the redirect does not work. I am logged in, the process works without errors. That's why when I refresh the same page(login) a session exists so I'm redirected by the php redirect.

$.post("login.php", {
  'variable1':$variable1,
  'variable2':$variable2
}).done(function() {
  window.location = "next-page-url";
}
});

UPDATED

$.post("login.php", {
  'variable1':$variable1,
  'variable2':$variable2
}).done(function() {
  window.location = "next-page-url";
});
janicehoplin
  • 397
  • 7
  • 15
  • Possible duplicate of [How can I make a page redirect using jQuery?](http://stackoverflow.com/questions/503093/how-can-i-make-a-page-redirect-using-jquery) – Sverri M. Olsen Apr 04 '16 at 04:30

2 Answers2

1

First off, your code is invalid. There is a } at the end, which shouldn't be there.

$.post("login.php", { // <- Open curly
    'variable1':$variable1,
    'variable2':$variable2
}).done(function() { // <- Close and open curly
    window.location = "next-page-url";
} // <- Close curly
}); // <- Close curly (was never opened...)

Try this instead:

$.post("login.php", {
    variable1: $variable1,
    variable2: $variable2,
}).done(function() {
    window.location = "next-page-url";
});

As for it not going to the new page, see this answer.

Community
  • 1
  • 1
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
  • I see that above I wrote it wrong but in my actual code, which looks just like you've shown, it doesn't work. I've tried both window.location.href and window.location. A simple alert('something'); works so it's executing properly. The redirect doesn't work. – janicehoplin Apr 04 '16 at 04:26
  • Unfortunately I'm still not able to redirect. It seems that I actually am not able to trigger a simple alert() within the .done function despite being correct as far as syntax goes. Does something need to return from the login.php file? The login.php file executes without problems as I am logged in, but the inside of .done is not triggered... thoughts? – janicehoplin Apr 04 '16 at 04:37
1

Your code has no major issue, as in just check other callback functions, like 'fail' or direct callback.

  <script>
    $(document).ready(function() {
      alert("hello");
      $.post("/", {
        "var": "hello",
        "bar": "qwerty",
      }).done(function() {
        alert("hello !!");
      }).fail(function() {
        window.location = "www.google.co.in";
      });
    });
  </script>
pavanjoshi
  • 240
  • 2
  • 10