I am looking for a way to start a function on form submit that would not leave the browser window waiting for the result.
我正在寻找一种方法来启动表单提交功能,不会让浏览器窗口等待结果。
Example: User fills in the form and press submit, the data from the form via javascript goes to the database and a function in php that will take several seconds will start but I dont want the user to be left waiting for the end of that function. I would like to be able to take him to another page and leave the function doing its thing server side.
示例:用户填写表单并按提交,表单中的数据通过javascript转到数据库,php中的一个函数将需要几秒钟才能启动,但我不希望用户等待该函数的结束。我希望能够把他带到另一个页面并让函数做服务器端。
Any thoughts?
Thanks
Thanks for all the replies...
感谢所有的答复...
I got the ajax part. But I cannot call ajax and have the browser move to another page.
我得到了ajax部分。但我不能调用ajax并让浏览器移动到另一个页面。
This is what I wanted.
这就是我想要的。
-User fills form and submits -Result from the form passed to database -long annoying process function starts -user carries on visiting the rest of the site, independent of the status on the "long annoying process function"
-User填写表单并提交-Result从表单传递到数据库-long恼人的进程函数启动-user继续访问站点的其余部分,独立于“长烦人的进程函数”的状态
By the way and before someone suggests it. No, it cannot be done by cron job
顺便说一句,有人建议之前。不,这不是由cron工作完成的
5 个解决方案
#1
Use AJAX to call the php script, and at the top of the script turn on ignore_ user_ abort.
使用AJAX调用php脚本,在脚本的顶部打开ignore_ user_ abort。
ignore_user_abort(true);
That way if they navigate away from the page the script will continue running in the backround. You can also use
这样,如果他们离开页面,脚本将继续在后台运行。你也可以使用
set_time_limit(0);
to set a time limit, useful if you know your script will take a while to complete.
设置时间限制,如果您知道脚本需要一段时间才能完成,则非常有用。
#2
The most common method is:
最常见的方法是:
exec("$COMMAND > /dev/null 2>&1 &");
#3
Ah, ok, well you're essentially asking therefore, does PHP support threading, and the general answer is no... however...
啊,好吧,你基本上要问的是,PHP是否支持线程,而一般的答案是否定的......然而......
there are some tricks you can perform to mimick this behaviour, one of which is highlighted above and involves forking out to a separate process on the server, this can be acheived in a number of ways, including the;
你可以通过一些技巧来模仿这种行为,其中一个突出显示在上面,并涉及分叉到服务器上的一个单独的进程,这可以通过多种方式实现,包括;
exec()
method. You also may want to look here;
方法。你也许想看看这里;
I have also seen people try to force a flush of the output buffer halfway through the script, attempting to force the response back to the client, I dont know how successful this approach is, but maybe someone else will have some information on that one.
我也看到有人试图在脚本中途强制刷新输出缓冲区,试图强制将响应强制回客户端,我不知道这种方法有多成功,但也许其他人会得到一些关于那个的信息。
#4
This is exactly what AJAX (shorthand for asynchronous JavaScript + XML) is for;
这正是AJAX(异步JavaScript + XML的简写)的用途;
It allows you to code using client side code, and send asynchronous requests to your server, such that the user's browser is not interuppted by an entire page request.
它允许您使用客户端代码进行编码,并向服务器发送异步请求,这样用户的浏览器就不会被整个页面请求所干扰。
There is alot of information relating to AJAX out there on the web, so take a deep breath and get googling!
网上有很多关于AJAX的信息,所以深呼吸,谷歌搜索!
#5
Sounds like you want to use some of the features AJAX (Asynchronous Javascript and XML - google) have to offer.
听起来你想要使用AJAX(异步Javascript和XML - 谷歌)必须提供的一些功能。
Basically, you would have a page with content. When a user clicks a button, javascript would be used to POST data to the server and begin processing. Simultaneously, that javascript might load a page from the server and then display it (eg, load data, and then replace the contents of a DIV with that new page.)
基本上,你会有一个内容页面。当用户单击按钮时,javascript将用于将数据POST到服务器并开始处理。同时,该javascript可能从服务器加载页面然后显示它(例如,加载数据,然后用该新页面替换DIV的内容。)
This kind of thing is the premise behind AJAX, which you see everywhere when you have a web page doing multiple things simultaneously.
这种事情是AJAX背后的前提,当你有一个网页同时做多个事情时,你会看到它。
Worth noting: This doesn't mean that the script is running "in the background on the server." Your web browser is still maintaining a connection with the web server - which means that the code is running in the "background" on the client's side. And by "background" we really mean "processing the HTTP request in parallel with other HTTP requests to give the feel of a 'background' running process"
值得注意的是:这并不意味着脚本“在服务器后台运行”。您的Web浏览器仍然与Web服务器保持连接 - 这意味着代码在客户端的“后台”中运行。而对于“背景”,我们的意思是“与其他HTTP请求并行处理HTTP请求,以给出'后台'运行过程的感觉”
#1
Use AJAX to call the php script, and at the top of the script turn on ignore_ user_ abort.
使用AJAX调用php脚本,在脚本的顶部打开ignore_ user_ abort。
ignore_user_abort(true);
That way if they navigate away from the page the script will continue running in the backround. You can also use
这样,如果他们离开页面,脚本将继续在后台运行。你也可以使用
set_time_limit(0);
to set a time limit, useful if you know your script will take a while to complete.
设置时间限制,如果您知道脚本需要一段时间才能完成,则非常有用。
#2
The most common method is:
最常见的方法是:
exec("$COMMAND > /dev/null 2>&1 &");
#3
Ah, ok, well you're essentially asking therefore, does PHP support threading, and the general answer is no... however...
啊,好吧,你基本上要问的是,PHP是否支持线程,而一般的答案是否定的......然而......
there are some tricks you can perform to mimick this behaviour, one of which is highlighted above and involves forking out to a separate process on the server, this can be acheived in a number of ways, including the;
你可以通过一些技巧来模仿这种行为,其中一个突出显示在上面,并涉及分叉到服务器上的一个单独的进程,这可以通过多种方式实现,包括;
exec()
method. You also may want to look here;
方法。你也许想看看这里;
I have also seen people try to force a flush of the output buffer halfway through the script, attempting to force the response back to the client, I dont know how successful this approach is, but maybe someone else will have some information on that one.
我也看到有人试图在脚本中途强制刷新输出缓冲区,试图强制将响应强制回客户端,我不知道这种方法有多成功,但也许其他人会得到一些关于那个的信息。
#4
This is exactly what AJAX (shorthand for asynchronous JavaScript + XML) is for;
这正是AJAX(异步JavaScript + XML的简写)的用途;
It allows you to code using client side code, and send asynchronous requests to your server, such that the user's browser is not interuppted by an entire page request.
它允许您使用客户端代码进行编码,并向服务器发送异步请求,这样用户的浏览器就不会被整个页面请求所干扰。
There is alot of information relating to AJAX out there on the web, so take a deep breath and get googling!
网上有很多关于AJAX的信息,所以深呼吸,谷歌搜索!
#5
Sounds like you want to use some of the features AJAX (Asynchronous Javascript and XML - google) have to offer.
听起来你想要使用AJAX(异步Javascript和XML - 谷歌)必须提供的一些功能。
Basically, you would have a page with content. When a user clicks a button, javascript would be used to POST data to the server and begin processing. Simultaneously, that javascript might load a page from the server and then display it (eg, load data, and then replace the contents of a DIV with that new page.)
基本上,你会有一个内容页面。当用户单击按钮时,javascript将用于将数据POST到服务器并开始处理。同时,该javascript可能从服务器加载页面然后显示它(例如,加载数据,然后用该新页面替换DIV的内容。)
This kind of thing is the premise behind AJAX, which you see everywhere when you have a web page doing multiple things simultaneously.
这种事情是AJAX背后的前提,当你有一个网页同时做多个事情时,你会看到它。
Worth noting: This doesn't mean that the script is running "in the background on the server." Your web browser is still maintaining a connection with the web server - which means that the code is running in the "background" on the client's side. And by "background" we really mean "processing the HTTP request in parallel with other HTTP requests to give the feel of a 'background' running process"
值得注意的是:这并不意味着脚本“在服务器后台运行”。您的Web浏览器仍然与Web服务器保持连接 - 这意味着代码在客户端的“后台”中运行。而对于“背景”,我们的意思是“与其他HTTP请求并行处理HTTP请求,以给出'后台'运行过程的感觉”