2

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

7 Answers 7

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+1, but instead of using the onclick-attribute, I would assign the event handler to the button in the jQuery code: $( '#update-button' ).on( 'click', function( event ) { $.ajax( /* … */ ); } ).
3

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:

Comments

1

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.

Comments

0

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)

Comments

0

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.

Comments

0

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.

Comments

-2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.