收到 POST 后触发函数
有没有办法在收到 $_POST 时触发函数?
我有一个 php 页面完成执行并调用另一个页面。另一个页面执行某些操作并将更新发布回此页面。我需要能够在收到 POST 时更新 div。
发出 ajax 请求并使用“成功”回调作为触发器不起作用,因为这只会更新我的 div 一次。问题是该页面将以不规则的时间间隔多次接收 POST,我需要一种方法在收到 POST 时触发操作。
Is there a way to trigger a function when a $_POST is received?
I have a php page that finishes executing and calls another page. The other page performs certain actions and POST updates back to this page. I need to be able to update a div as and when a POST is received.
Making an ajax request and using "success" callback as a trigger doesn't work since this will update my div ONLY ONCE. The problem is that this page will receive POST multiple times at irregular intervals and I need a way to trigger an action whenever POST is received.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 Ajax(我会使用 jQuery)并设置一个时间间隔来检查状态是否发生变化。 “其他页面”不应回发到第一页,而应将数据保存在“可请求”区域(可能是数据库、文件等),其中将通过 Ajax 请求定期存储和检索此新信息/状态。
Use Ajax (I'd use jQuery) and set a time interval to check if there was a status change. The "other page" should not post back to the first page, but save data in a "requestable" area (might be an database, a file etc) where this new info/status will be stored and retrieved periodically by the Ajax request.
使用AJAX,因为在php中无法触发
$_POST
接收,据我了解,您只需要在收到帖子后更新一些
内部 html,并且您可以使用 jQuery 活动
Use AJAX, because you can't trigger
$_POST
recieving in php,As I understand you just need to update some
<div>
inner html after post is recieved, and you are able to do it with jQuery events我会按以下方式执行此操作(下面是 PHP 代码,仅供参考):
但是 AJAX 可能更合适,并且(无论您相信与否)更灵活。
AJAX 调用将减少您服务器上的负载,并且可以一次或多次更新框(取决于您如何编写 JS 代码) - 在这种情况下,为了简单起见,我建议使用 jQuery(如果您不熟悉 JS)。
I would do this in the following way (below is the PHP code, just FYI):
However AJAX may be more appropriate and it is (believe or not) more flexible.
AJAX call will be smaller load on your server and can update the box single or multiple times (depending on how you write the JS code) - in such case I recommend using jQuery for simplicity (if you are not familiar with JS).
通常,我会做这样的事情...
$var_posted=0;
if($_POST['var']){$var_posted=1;}
然后.. .
if($var_posted==1){some_function();}
usually, I would do something like this...
$var_posted=0;
if($_POST['var']){$var_posted=1;}
then...
if($var_posted==1){some_function();}