简体   繁体   English

如何在另一个PHP脚本(如更新按钮)中在后台运行php脚本

[英]How to run a php script in the background in another php script (like an update button)

How would I go about running a php script when I pressed say an "update" button, in which then it would run say script x1.php (which has no echo's or or other output, successes or failed) then update the current page (I know the update part can be done with ajax, but im not sure how and how I would get the x1.php script to run in the background and trigger the ajax update when done). 当我按下“更新”按钮时,我将如何运行php脚本,然后在其中运行脚本x1.php(没有回显或其他输出,成功或失败),然后更新当前页面(我知道更新部分可以用ajax完成,但是我不确定如何以及如何让x1.php脚本在后台运行并在完成时触发ajax更新。

Any pointers in the right direction would be great :) 任何指向正确方向的指针都将是很好的:)

You can try like this: 您可以这样尝试:

<input type="button" onclick="go();" value= "Update" />


function go()
{
    $.ajax(
        {
               type: "POST",
               url: "script x1.php",
               data: data, // data to send to above script page if any
               cache: false,

               success: function(response)
               {
                // update code for your page
               }
         });
}

This will run your script in the background and then you can also update the contents of the page. 这将在后台运行您的脚本,然后您还可以更新页面的内容。 You might want to modify the code as per your needs. 您可能需要根据需要修改代码。

which has no echo's or or other output, successes or failed 没有回声或其他输出,成功或失败

There should be some response sent back for the above script to be able to know that it has finished running otherwise i am afraid you can't find out when and how script ended. 应该为上面的脚本发送回一些响应,以便能够知道它已完成运行,否则,恐怕您无法确定脚本何时以及如何结束。

Note : Using JQuery here. 注意 :此处使用JQuery。

Ajax is the 100% correct answer here. Ajax是这里100%正确的答案。 You generate a HTML page that makes an Ajax request to x1.php. 您生成一个HTML页面,该页面向x1.php发出Ajax请求。 Depending on what that returns, the Ajax success method updates the page. 根据返回的内容,Ajax success方法将更新页面。

To be exact, you make the request using the Ajax method in JavaScript, and update the page using JavaScript. 确切地说,您可以使用JavaScript中的Ajax方法发出请求 ,然后使用JavaScript更新页面。

Here's some examples and documentation: 以下是一些示例和文档:

AJAX is the "right" direction. AJAX是“正确”的方向。 You should call the x1.php, and use it's output to update the current page. 您应该调用x1.php,并使用其输出来更新当前页面。 Take care about your site should work without javascript. 请注意您的网站应在没有javascript的情况下正常工作。

What you're actually asking for is exactly what AJAX is all about. 您真正要的正是AJAX的全部含义。 You would just attach an event observer to that button and run your PHP script, returning whatever you need. 您只需将事件观察器附加到该按钮并运行PHP脚本,即可返回所需的内容。 The exact syntax is just dependent on whether you are using a JavaScript lib (jQuery/Prototype etc) 确切的语法仅取决于您是否使用JavaScript库(jQuery / Prototype等)

Have a look at an question I asked. 看看我问的一个问题。 Return value to browser but still process in PHP . 将值返回给浏览器,但仍使用PHP处理

Use ajax to make the request, but get PHP to return a value even though it is still processing in the background. 使用ajax发出请求,但是让PHP返回一个值,即使该值仍在后台处理。

You can employ a variety of mechanisms here. 您可以在此处采用多种机制。 Judging by your question, i take it the "Update" process is expected to take a bit longer than you would expect from an usual ajax request? 从您的问题来看,我认为“更新”过程将比您从普通的ajax请求中获取的时间更长一些?

  • Option 1 选项1

    Make the update script write the status to a database, then poll for update status via AJAX 使更新脚本将状态写入数据库,然后通过AJAX轮询更新状态

  • Option 2 选项2

    Make the update script create a lockfile while it's running, and delete the lockfile once it has finished, use AJAX to poll for exitance of the lockfile 使更新脚本在运行时创建一个锁文件,并在完成后删除该锁文件,使用AJAX轮询锁文件的退出状态

  • Option 3 选项3

    If the update script is expected to finish updating quickly, simply echo an 'success' or 'error' status message and handle it on successful AJAX request as Sarfraz suggested. 如果希望更新脚本能够快速完成更新,则只需回显“成功”或“错误”状态消息,并按照Sarfraz的建议在成功的AJAX请求后进行处理。

Just include a .php that has as SQL-query something like this: mysql_query("SELECT * FROM table WHERE date < DATE_SUB(NOW(), INTERVAL 8 DAY)"); 只需包含一个具有如下SQL查询内容的.php文件即可:mysql_query(“ SELECT * FROM table WHERE date <DATE_SUB(NOW(),INTERVAL 8 DAY)”);

It will only be executed if that statement is true. 仅当该语句为true时才会执行。 :) :)

In that .php, make sure not to put any echo's. 在该.php文件中,请确保不要放置任何回声。 It will work 'automatically' and the user won't notice a thing. 它会“自动”运行,并且用户不会注意到任何事情。

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

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