The user submits a form, gets a csv file and is shown the form again. <?php if(isset($_POST['myform'])) { header("Content-type: text/csv"); header("Content-Disposition: attachment; filename=file.csv"); header("Pragma: no-cache"); header("Expires: 0"); echo <csv data>; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>TITLE</title> </head> <body> <MY FORM> </body> </html> Code (markup): That is the relevant part of the code. The problem is that the all the html code from the doctype down to </html> is also being printed to the csv file as well as the screen. I know I can post the form to another script, send the csv, and redirect back to the form after. I also know that I can create the csv, save it server side, and provide the user with a download link without having to worry about headers being an issue. I am trying to avoid all this. I also tried wrapping the csv creation in an output buffer but that doesn't help because after I flush the buffer, I am still sending the html to the screen. Is there a way that I can possibly tell it where to stop putting the data in the csv? I know I cannot change the headers and thats ok because the form is still showing like I want it to (even with the wrong headers). Im just wondering if there is a function or something that says hey stop putting data in the csv and give it the user already. Ignore the rest of this crap. I know this may seem very trivial and there are tons of ways around it but this is part of a much larger project I am working on and there is a specific reason I want to keep all the code in a single script, not save the files to the server, and not use redirects. I am guessing I may need to use js to fix this or post the form with a query string then redirect back but any advice you have would help. Thanks in advance.
Try this: let the action be the script URL that will process the form data and show the CSV file. This way, when I fill in the form and press the button, the download alert will show in the same window and require no further action from your side!