views:

535

answers:

3

I have a web form that uses complete online. When they press submit it will start a file download for them.

At the moment, I process the form submission and generate a suitable file for the user and fire it off with suitable headers. eg...

header('Content-type: "application/octet-stream"');
header('Content-Disposition: attachment; filename="yourfile.txt"');
header("Content-Transfer-Encoding: binary");

However, since this starts a download right away, the original form is left on screen by the browser.

I would really like to go to some "Thank you" screen once the download completes (or before the download starts). I know it is possible, because almost every download site you visit does this (normally to pump you full of adverts before the download starts).

So, How do I show a "Thank You" screen that starts the download after a second?

How would any solution proposed effect the behaviour of the back button, as I don't want the file downloading again without the form being refilled?

I am using PHP on the server and can rely on Javascript (and jQuery) being available on the client.

Thank you.

+8  A: 

You could send the form to the Thank you document and put there a META refresh to the file download:

<meta http-equiv="refresh" content="3;url=download.php">
<p>Thank you! The download will start in 3 seconds. If not, use this link to download the <a href="download.php">file</a></p>
Gumbo
Meta redirect is nice, clean and simple.
middus
Thanks! Finally the solution I was looking for.
Ula Karzelek
+1  A: 

Add a second page that says something "thank you, your donwnload will start in a few seconds" and triggers the download using javascript:

$(document).ready(function(){
  window.setTimeout(function(){
    window.location = 'http://yourdownloadhost.com/file.zip';
  }, 1500);
});

or use a meta redirect.

middus
Why use javascript when a meta redirect works too?
Georg
Well, as you can see from my comment to Gumbo's post, I myself prefer META redirect. However, I was kind of mislead by the OP's "can rely on Javascript (and jQuery)".However, some templating system's make it hard to inject stuff into the <head>. So sometimes JS might be an option.
middus
Ought to be in the header too. AFAIK javascript in the body are illegal.
Georg
No, JS in the body is perfectly fine. See, e.g., Google Analytics.
middus
A: 

You can insert a hidden iframe into your page and submit your form to this iframe.

Andrew