views:

997

answers:

7

Hi:

I am looking for a way to start a function on form submit that would not leave the browser window waiting for the result.

Example: User fills in the form and press submit, the data from the form via javascript goes to the database and a function in php that will take several seconds will start but I dont want the user to be left waiting for the end of that function. I would like to be able to take him to another page and leave the function doing its thing server side.

Any thoughts?

Thanks

+1  A: 

The most common method is:

exec("$COMMAND > /dev/null 2>&1 &");
Frank Farmer
how do I set that on my code?<input type="button" onsubmit="exec()">?
Hugo Gameiro
hugo, that is server side code, ie. the code that responds to a browser request, and which resides on the server. You are talking about client side code (probably javascript), in which case see the answers below relating to AJAX
simon622
This is also an option, though it is typically one that web developers try to avoid when creating "everyday" applications. Having a PHP script execute shell commands has the potential to create big security risks. Eg, if "command" is "cat" and the user is allowed to supply a file to cat, one could execute `cat /etc/passwd` which might be undesirable!
rascher
Thanks... I will look into this exec() from php... I think this is exactly what I want
Hugo Gameiro
A: 

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

AJAX Information

It allows you to code using client side code, and send asynchronous requests to your server, such that the user's browser is not interuppted by an entire page request.

There is alot of information relating to AJAX out there on the web, so take a deep breath and get googling!

simon622
A: 

Sounds like you want to use some of the features AJAX (Asynchronous Javascript and XML - google) have to offer.

Basically, you would have a page with content. When a user clicks a button, javascript would be used to POST data to the server and begin processing. Simultaneously, that javascript might load a page from the server and then display it (eg, load data, and then replace the contents of a DIV with that new page.)

This kind of thing is the premise behind AJAX, which you see everywhere when you have a web page doing multiple things simultaneously.

Worth noting: This doesn't mean that the script is running "in the background on the server." Your web browser is still maintaining a connection with the web server - which means that the code is running in the "background" on the client's side. And by "background" we really mean "processing the HTTP request in parallel with other HTTP requests to give the feel of a 'background' running process"

rascher
A: 

Thanks for all the replies...

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

This is what I wanted.

-User fills form and submits -Result from the form passed to database -long annoying process function starts -user carries on visiting the rest of the site, independent of the status on the "long annoying process function"

By the way and before someone suggests it. No, it cannot be done by cron job

Hugo Gameiro
+1  A: 

Ah, ok, well you're essentially asking therefore, does PHP support threading, and the general answer is no... however...

there are some tricks you can perform to mimick this behaviour, one of which is highlighted above and involves forking out to a separate process on the server, this can be acheived in a number of ways, including the;

exec()

method. You also may want to look here;

PHP threading

I have also seen people try to force a flush of the output buffer halfway through the script, attempting to force the response back to the client, I dont know how successful this approach is, but maybe someone else will have some information on that one.

simon622
A: 

And does anyone know if I can call a php file and make it act alone via exec() ?

Hugo Gameiro
exec("php myscript.php > /dev/null 2>You'll have to have php CLI installed, of course.
Frank Farmer
+1  A: 

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

ignore_user_abort(true);

That way if they navigate away from the page the script will continue running in the backround. You can also use

set_time_limit(0);

to set a time limit, useful if you know your script will take a while to complete.

Jacob
cool... I like this :)
Hugo Gameiro