I have a script that does a jquery AJAX post and then should redirect to a different page. The problem is that it is sometimes redirecting before the post completes. There seems to be a couple of solutions but I can’t get any of them to work. Thanks for your help!
Here is my code:
$.post("cart.php", { 'products[]':postvalues }, function(data) { });
location.href="index.php";
You need to put your
location.hrefinside the callback function.See the jQuery.post docs for more information.
Although, I must ask: if you are going to redirect anyway, why do this via ajax?
Is there some reason you don’t want to simply submit a form the normal way? If not, you should probably go that route instead.