Forum Moderators: coopster

Message Too Old, No Replies

Executing a PHP script in the background

(once a standard button has been clicked)

         

dwhite

9:35 am on Jan 5, 2006 (gmt 0)

10+ Year Member



Using a standard form button, I want the user to be able to click the button to execute a PHP script without the screen refreshing. What will happen is that the PHP script does some stuff to create a file, and then sends back a file to the user.

How do I:
A: Allow the button to be clicked and the form fields to be sent to a PHP without the screen trying to refresh to a new page.

B: From that background script, get PHP to send a file from the server to the user, again without refreshing to a new page. Instead it would just bring up a file requester to allow the user to save the file.

sprinkles

12:58 pm on Jan 5, 2006 (gmt 0)

10+ Year Member



You need to use AJAX for this.

[edited by: coopster at 3:00 pm (utc) on Jan. 5, 2006]
[edit reason] removed url per TOS [webmasterworld.com] [/edit]

jatar_k

4:25 pm on Jan 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



why the no refresh?

dwhite

7:13 pm on Jan 5, 2006 (gmt 0)

10+ Year Member



Sprinkles: Is it because PHP is incapable of running a script in the background?

At least, is it possible for a PHP script to send a file to the user, and if so, how. I've looked on Google for a while, and didn't seem to find a solution.

Jatar_k: When I said refresh, I meant no changing of the current page. It's so that the user can submit new variables and get another file later, without having to use the back button in the browser.

jatar_k

11:54 pm on Jan 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



http is a stateless protocol, sometimes I just like saying that ;)

problem is that to send information to the server it needs to actually send it

once you client has gotten the page (initial GET request) they will need to POST their info to the server and this will go to another script, normally, and that script will then send something back

you can then do all the things you want to and then redisplay the form if you like

dwhite

7:35 am on Jan 6, 2006 (gmt 0)

10+ Year Member



Okay, thanks both of you for your help!

I'll try the AJAX solution, but if I have no luck with that, then I'll try the redisplaying of the form.

sprinkles: Hope you got my stickymail, only WebmasterWorld says there's nothing in the outbox.

Oh I still need to know how I can get PHP to send the user a file...

jatar_k

4:19 pm on Jan 6, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



when you say send do you mean

display it for viewing in the browser?
let them download it?
or actually send it via email?

whoisgregg

6:07 pm on Jan 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can POST to an iframe on the same page as your form by using target="name_of_iframe" in your <form> tag.

Since you are only using the iframe to return a download prompt, the iframe could be styled (using CSS) to be 1px by 1px and positioned off the page.

This gives the appearance of "no refresh" without dependence on javascript (without AJAX).

dwhite

7:12 pm on Jan 6, 2006 (gmt 0)

10+ Year Member



display it for viewing in the browser?
let them download it?
or actually send it via email?

Let them download it, by bringing up a save file requester. I'm hoping that this isn't another 'impossibility' in the same way that the 'button click without changing page' problem was.

whoisgregg

4:20 pm on Jan 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Who said anything was impossible? :)

Send the right header [php.net] (Content-Disposition) and the browser should show a save dialog.

dwhite

10:15 am on Jan 15, 2006 (gmt 0)

10+ Year Member



Almost there. I must have tried a hundred different combinations. In each case, either a save/open request doesn't pop up at all, or the Explorer window closes immediately afterwards (I want a bit of HTML to display afterwards).

I've been playing around with stuff like this:

header("Content-Type: audio/x-midi");
header("Content-Disposition: attachment"); // (tried inline too)
header("Location:$outname");

In this case, a requester appears which is great, but as soon as the file is opened or is saved, the Explorer window immediately closes, and therefore doesn't display the HTML code I want people to see.

It turns out you can get rid of the first two lines completely, so you're just left with:
header("Location:$outname");

Again, I have the same problem.

whoisgregg

7:39 pm on Jan 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It turns out you can get rid of the first two lines completely

Hmm, depends on the browser and how the user has set up their mime-type handling. Best to leave your "this is a download" headers intact.

So, the download works, but now you want to show HTML after the file is done downloading? Or just while it's downloading?

dwhite

11:41 pm on Jan 21, 2006 (gmt 0)

10+ Year Member



So, the download works, but now you want to show HTML after the file is done downloading? Or just while it's downloading?

Either will do actually. As long as if they 'cancel' or close the requestor*/save window, the html will still then display.

(* NB. The 'requestor' term I used is the window which says "would you like to open or save this file?")