简体   繁体   English

使用ajax提交表单,但重定向到另一个页面

[英]Submit form with ajax but redirect to another page

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); 我应该在html和jquery中使用什么,如果我想在表单上提交(a)执行某个php文件(url-a); (b) redirect to another one.(url-b) (b)重定向到另一个。(url-b)

I currently implemented the jquery form plugin. 我目前实现了jquery表单插件。 http://jquery.malsup.com/form/#getting-started http://jquery.malsup.com/form/#getting-started

Like so: 像这样:

step a. 步骤a。 included jquery and form plugin scripts; 包括jquery和表单插件脚本;

step b. 步骤b。 in html, in the form element: 在html中,在表单元素中:

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

step c. 步骤c。 in js, inside document ready : 在js中,内部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.. 假设url-a是ajax应该处理的那个,我想重定向到url-b ..

Thank you! 谢谢!

$('#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 如果您希望页面重定向,那么为什么您希望它提交ajaxly任何成功处理程序中的方法,您可以重定向到页面

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

in the "complete" function of $.post(..) you should change the location of the browser. $.post(..)的“完整”功能中,您应该更改浏览器的位置。 You can't use a server-side redirect with ajax. 您不能使用带有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. 如果你想在ajax完成之前重定向,这是可能的,但很难。 I don't know how to do it in PHP, for example. 例如,我不知道如何在PHP中执行此操作。 The thing is, if you redirect immediately (again should be done via javascript), then the request is aborted from the client. 问题是,如果你立即重定向(再次应该通过javascript完成),那么请求将从客户端中止。 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. 要么不重定向,要么不在上一页上启动ajax请求 - 一旦新页面加载就启动它。

Update: I saw you need your ajax request to run for an hour - that won't happen - the browser will timeout. 更新:我看到你需要你的ajax请求运行一个小时 - 这不会发生 - 浏览器将超时。 After you confirm that one hour is really needed, check this for asynchronous php tasks. 在确认确实需要一个小时后, 请检查此异步php任务。 You can start it from $(document).ready(function() {..}); 你可以从$(document).ready(function() {..}); of the 2nd page 第二页

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM