tags:

views:

102

answers:

6

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 :)

+1  A: 

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:

Pekka
A: 

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.

erenon
+2  A: 

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.

Sarfraz
A: 

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)

seengee
A: 

Have a look at an question I asked. http://stackoverflow.com/questions/2171003/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.

Louis
A: 

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.

code_burgar