Sean
Sean

Reputation: 269

jQuery Post and Redirect on Success

I have searched the internet for an answer and so far found lots of different ones.

Say I have a jQuery Post that looks something like this:

$.post('test.php',{data:data},function(){
    document.location = 'success.php';
})

Then on my test.php page I have a bunch of database inserts. Will this code still continue to execute once the page redirects?

I would have thought it should but I have so far found some people saying it will, others that it won't and others saying it might.

Could someone please clear this up for me?

Upvotes: 2

Views: 4285

Answers (2)

Vivek Sharma
Vivek Sharma

Reputation: 597

You can try with this sure this will work ---

$.post('test.php',{data:data}).done(function(data){
   window.location.href="success.php";
});

Upvotes: 3

MarcinWolny
MarcinWolny

Reputation: 1645

jQuery post documentation states that your function is:

A callback function that is executed if the request succeeds. Required if dataType is provided, but can be null in that case.

which means that it'll be called only after client (jQuery) receives complete, successful response from the server - in your case the redirect will occur only after PHP has completed execution of success.php script and all database inserts are done.

Upvotes: 2

Related Questions