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.
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!
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./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!
Share Improve this question edited Jan 10, 2012 at 22:44 Bozho 597k147 gold badges1.1k silver badges1.2k bronze badges asked Jan 7, 2012 at 8:51 Lucy WeatherfordLucy Weatherford 5,54416 gold badges52 silver badges78 bronze badges3 Answers
Reset to default 3$('#form').ajaxForm(function() {
$.post("url-a.php", {a: a, b: b});
window.location.href="/url-b.php";
});
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";
});
in the "plete" 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 pleted, 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