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).
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.
Ajax is the 100% correct answer here. You generate a HTML page that makes an Ajax request to x1.php. Depending on what that returns, the Ajax success
method updates the page.
To be exact, you make the request using the Ajax method in JavaScript, and update the page using JavaScript.
Here's some examples and documentation:
- Using JQuery: jQuery.ajax()
- Using Prototype: Introduction to Ajax
AJAX is the "right" direction. You should call the x1.php, and use it's output to update the current page. Take care about your site should work without javascript.
What you're actually asking for is exactly what AJAX is all about. You would just attach an event observer to that button and run your PHP script, returning whatever you need. The exact syntax is just dependent on whether you are using a JavaScript lib (jQuery/Prototype etc)
Have a look at an question I asked. Return value to browser but still process in PHP.
Use ajax to make the request, but get PHP to return a value even though it is still processing in the background.
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?
Option 1
Make the update script write the status to a database, then poll for update status via AJAX
Option 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
Option 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.
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)");
It will only be executed if that statement is true. :)
In that .php, make sure not to put any echo's. It will work 'automatically' and the user won't notice a thing.
精彩评论