views:

57

answers:

2

Hello all,

I am making use of JQuery to fire off an AJAX request. As soon as that PHP script is initiated I want to redirect my browser to a different page. But the AJAX request must have been fired off successfully, it doesn't matter what that scripts returns. Currently I do the following, but it redirects instantly whilst the script was requested partially.

$.get('process.php');
location.href = 'result.php';

Thanks all for any help

+4  A: 

Using the callback should work...

$.get('process.php', null, function(){ location.href = 'result.php'; });
Quintin Robinson
The call back waits for the AJAX request to complete and return somethng, meaning the page will be redirected after the other PHP script has come back with something. This is not what I need to happen. I just want to make a successful request and redirect? Maybe this should be done via PHP?
Abs
It will work, but doesn't address the OP's question.
Doug Neiner
@Abs You might want to handle it server side, I don't know exactly the reasoning you have for this, but it does seem like an odd thing to do.
Quintin Robinson
@Quintin I guessed as much. Server side it is. Btw, it is very common to run something in the background whilst the user does something else which is what I am trying to achieve! :) I just happen to be using Javascript!
Abs
Why not let your PHP spawn a second process, and return early for the first? Then you will have confirmation of the request but also can run your other tasks in the background
K Prime
+1  A: 

You can use the current JavaScript method that your are using but will have to modify your PHP script to ignore the early abort with ignore_user_abort(true).

Otherwise, you will have to use a callback (as shown by Quintin) to ensure that the PHP script that you are requesting is fully executed.

Justin Johnson
This helped alot: ignore_user_abort(true) coupled with ob_start() at the top. Thank you. :)
Abs
Glad I could help
Justin Johnson