In background function send mode php running

I'm looking for a way to run a function on a submit form that won't leave the browser window waiting for the result.

Example: A user fills out a form and hits submit, the data from the form is submitted via javascript to the database and the php function, which takes a few seconds, will run, but I don't want the user to be left waiting for the end of this function. I would like to be able to take it to another page and leave the function doing its own server side.

Any thoughts?

thanks


Thanks for all the answers ...

I got the ajax part. But I cannot call ajax and move the browser to another page.

This is what I wanted.

-User fills out the form and submits -Message from the form submitted to the database - long annoying process function starts -user continues to visit the rest of the site, regardless of status on "long annoying process function"

By the way, and before someone tells it. No, this cannot be done with a cron job

+1


a source to share


5 answers


Use AJAX to call the php script, and at the top of the script, include ignore_ user_ abort .

ignore_user_abort(true);

      

This way, if they move away from the page, the script will continue to run in the background. You can also use



set_time_limit(0);

      

to set a time limit is helpful if you know your script will take a while.

+3


a source


The most common method:



exec("$COMMAND > /dev/null 2>&1 &");

      

+1


a source


Ah, okay, you're essentially asking if PHP supports streams, and the general answer is no ... however ...

there are some tricks you can perform to simulate this behavior, one of which is highlighted above and involves forking to a separate process on the server, this can be achieved in several ways, including:

exec()

      

method. You can also look here:

PHP streams

I've also seen people trying to force an output buffer stream halfway through a script trying to return a response to the client, I don't know how successful this approach is, but maybe someone will have information on this.

+1


a source


This is exactly what AJAX (shorthand for Asynchronous JavaScript + XML) is for;

AJAX info

It allows you to code client-side code and send asynchronous requests to your server so that the user's browser is not intercepted by the whole page request.

There is a lot of information on the internet regarding AJAX, so take a deep breath and get googling!

0


a source


It sounds like you want to use some of the AJAX features (asynchronous Javascript and XML-google).

Basically, you will have a content page. When the user clicks the button, the javascript will be used for the POST data on the server and start processing. At the same time, this javascript can load the page from the server and then display it (for example, load the data and then replace the content of the DIV with this new page.)

This kind of thing is a premise for AJAX that you see everywhere when you have a web page doing multiple things at once.

It is worth noting that this does not mean that the script is running "in the background on the server". Your web browser still maintains a connection to the web server - this means the code is running in the background from the client side. And by "background" we really mean "processing an HTTP request in parallel with other HTTP requests to give an idea of ​​the" background "process running.

0


a source







All Articles