3

What should I use in the html and in the jquery if I want upon form submit to (a) execute a certain php file (url-a); (b) redirect to another one.(url-b)

I currently implemented the jquery form plugin. http://jquery.malsup.com/form/#getting-started

Like so:

step a. included jquery and form plugin scripts;

step b. in html, in the form element:

      <form action="url-a.php">
          <!--form code inserted here including input elements, etc and submit-->
      </form>

step c. in js, inside document ready:

      $('#form').ajaxForm(function() { 
           $.post("url-a.php", {a: a, b: b});
      }); 

Should one of these url links be changed? assume that url-a is the one the ajax should take care of, and I want to redirect to url-b..

Thank you!

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
Lucy Weatherford
  • 5,452
  • 16
  • 50
  • 76

3 Answers3

3
$('#form').ajaxForm(function() { 
  $.post("url-a.php", {a: a, b: b});
  window.location.href="/url-b.php";
});
roselan
  • 3,755
  • 1
  • 20
  • 20
1

if you want the page to redirect then why do you want it to submit ajaxly any how in the success handler you can redirect to the page

 $.post("url-a.php", {a: a, b: b},function(){
   window.location.href="/page/to/redirect.php";
 });
Rafay
  • 30,950
  • 5
  • 68
  • 101
  • because that's what my app needs, I need the redirect to happen BEFORE the ajax returns. so this solution won't work. (the ajax is expected to be more than just a few seconds) – Lucy Weatherford Jan 07 '12 at 09:07
  • why would you want to redirect and then submit the form, for that you will have to serialize the form and take it to the next page then submit but that is not the way to do it what if the form submission fails due to numerous reasons – Rafay Jan 07 '12 at 09:09
  • i don't want necessarily to first redirect OR first submit, they are both the same for me, the point is I don't want to wait for the success function of the submit's ajax. This is because what that php file is doing with the submit results is very complicated and can take anywhere from minutes to several hours. So I want to user not to be bothered with that. He did submit, and he went somewhere else, whatever this php page is doing in the background should nt be in the current browser's concern. – Lucy Weatherford Jan 07 '12 at 09:46
  • ok I'll try the second approach. So how should I do it? use `serialize`? – Lucy Weatherford Jan 07 '12 at 17:57
0

in the "complete" function of $.post(..) you should change the location of the browser. You can't use a server-side redirect with ajax.

 $.post("url-a.php", {a: a, b: b},function(){
      window.location.href="/url-b.php";
 });

If you want to redirect before the ajax has completed, that's possible, but hard. I don't know how to do it in PHP, for example. The thing is, if you redirect immediately (again should be done via javascript), then the request is aborted from the client. And if the request is aborted, the server aborts it too. Sometimes it is possible to start a new thread on the server that is separate from the request thread, but if the request is aborted before it fully reaches the server, it will all fail.

In general, you should not do this - it's a wrong approach. Either don't redirect, or don't start the ajax request on the previous page - start it as soon as the new page loads.

Update: I saw you need your ajax request to run for an hour - that won't happen - the browser will timeout. After you confirm that one hour is really needed, check this for asynchronous php tasks. You can start it from $(document).ready(function() {..}); of the 2nd page

Community
  • 1
  • 1
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • I want the redirect to happen immediately though, and the ajax to just keep running and do its thing for a while... – Lucy Weatherford Jan 07 '12 at 09:08
  • Okay I'm trying this last approach you mentioned - start the ajax as soon as the new page loads. but it isn't working... where should I put the jqury `post`? under what? just doc on load? and how should I pass the variables? I used post and then passed them to js again, but this seems redundant... thanks! – Lucy Weatherford Jan 08 '12 at 06:14
  • I asked a new question about it - see here: http://stackoverflow.com/questions/8775868/submit-form-then-send-form-data-to-ajax-from-a-new-page – Lucy Weatherford Jan 08 '12 at 07:14