radi8 Posted January 21, 2011 Share Posted January 21, 2011 For those willing to run this sample problem, reference the 2 files below. You will need to create an Excel file called blank.xls and put it in the same directory as these 2 files. When you launch indexTest.php, it will load the formTest.html.php page. This form has 3 options on it: 1. Export Excel - will prompt you to download the Excel file you made and should clear the error message on the form 2. Test Flag - tests the functionality of clearing the error message, shows what should happen when the Export Excel option is selected 3. Reset - resets back to initial state I really need the Export Excel option to be allow the form to reload properly. This is a problem that I have been wresting with for awhile and need a solution. Hopefully someone can give me some insight on how to get this to work. indexTest.php <?php $frmErrorLevel=1; $frmErrMsg='Error Level 1 indicated'; if(isset($_POST['action']) && $_POST['action']=='submitted') { if(isset($_POST['ExportCarrier'])) { $download_filename = "blank.xls"; $FileInfo = pathinfo($download_filename); // fix for IE catching or PHP bug issue header("Pragma: public"); header("Expires: 0"); // set expiration time header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); // browser must download file from server instead of cache // force download dialog header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-type: application/x-msexcel"); header("Content-Type: application/download"); // use the Content-Disposition header to supply a recommended filename and // force the browser to display the save dialog. header("Content-Disposition: attachment; filename=".$download_filename.";"); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($download_filename)); @readfile($download_filename); //ob_end_clean(); $frmErrorLevel=0; } if(isset($_POST['Test_Flag']) && $_POST['Test_Flag']=='Test Flag'){ $frmErrorLevel=0; $frmErrMsg=''; } } include ('formTest.html.php'); exit(); ?> formTest.html.php <?php ini_set ("display_errors", "1"); error_reporting(-1); echo "<pre>"; echo "--> Form data (POST) <-- <br>"; print_r ($_POST); echo "--> Form data (GET) <-- <br>"; print_r ($_GET); echo "</pre>"; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Export Trucking Data</title> </head> <body> <h1>Export Data</h1> <form action="" method="post"> <?php if($frmErrorLevel>0) { echo '<font color=#CC6600 size=+1>'.$frmErrMsg.'</font><br><br>'; } ?> <table width="650" border="0"> <caption> <font size="+3">Data Export Options</font> </caption> <tr> <td> <?php if($frmErrorLevel==1) print '<img src= "..\..\images\rdx.gif">'; else echo " "; ?> </td> <td width="275"><label> Carrier table data</label></td> <td width="100"><input name="ExportCarrier" type="submit" value="Export Excel"></td> <td> <div align="center"> <td><input type="submit" name="Test Flag" value="Test Flag"></td> <td> <div align="center"> <td><input type="submit" name="Reload Form" value="Reset"></td> </tr> </table> <div> <input type="hidden" name="action" value="submitted" /> </div> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/225252-form-reload-problem/ Share on other sites More sharing options...
BlueSkyIS Posted January 21, 2011 Share Posted January 21, 2011 STILL the "can't send 2 different file streams to one browser" problem? Dude! (or Dude-ette), it can't be done with plain PHP and javascript. You would need some Java or Flash to monitor the download status of the incoming file (if that's even possible), then fire off something for the message. But you CANNOT have PHP send an Excel file to the browser and then have PHP send another file (HTML with a form, or anything else) to the browser automatically. You get ONE shot: the Excel file OR HTML, not both. Quote Link to comment https://forums.phpfreaks.com/topic/225252-form-reload-problem/#findComment-1163319 Share on other sites More sharing options...
BlueSkyIS Posted January 21, 2011 Share Posted January 21, 2011 On the other hand: If you can find a website that does it the way you describe, I will tell you how they do it. Quote Link to comment https://forums.phpfreaks.com/topic/225252-form-reload-problem/#findComment-1163320 Share on other sites More sharing options...
radi8 Posted January 21, 2011 Author Share Posted January 21, 2011 Well, that answers the question quite nicely. If it cannot be done the way I have it, is there another way to get something similar to work? There MUST be a solution somewhere, somehow. People get Excel files all the time. Quote Link to comment https://forums.phpfreaks.com/topic/225252-form-reload-problem/#findComment-1163329 Share on other sites More sharing options...
BlueSkyIS Posted January 21, 2011 Share Posted January 21, 2011 Downloading excel files is no problem. the problem is "I really need the Export Excel option to be allow the form to reload properly." Doing anything 'after the excel file is downloaded' is the problem. Quote Link to comment https://forums.phpfreaks.com/topic/225252-form-reload-problem/#findComment-1163333 Share on other sites More sharing options...
radi8 Posted January 21, 2011 Author Share Posted January 21, 2011 As I have been finding out... It just seems like there should be someway to force the page to reload from scratch. Gotta think outside this box for awhile. I refuse to let little problems like this kick my ass. Thanks BlueSkyIS, hit that pineapple bong a few times for me, I need it right now. Quote Link to comment https://forums.phpfreaks.com/topic/225252-form-reload-problem/#findComment-1163338 Share on other sites More sharing options...
PFMaBiSmAd Posted January 21, 2011 Share Posted January 21, 2011 Have you tried putting the form in its own file and the download portion of the code in its own file (which you will need to do anyway as you can only output the headers followed by the file contents) AND then using an onclick event for the Export Excel button that would request the download link and also trigger AJAX to update the form. Javascript on your form page - <script type="text/javascript"> function download() { window.location.href='dl.php'; // Other code to trigger AJAX to read the status from the server and update this form. // The dl.php code is what outputs the headers followed by the file contents and will update the status in a session variable that the AJAX will get and display. } </script> The button - <input name="ExportCarrier" type="button" value="Export Excel" onClick=download();> The above parts do what you expect (less any actual AJAX code.) Searching the Internet for 'onclick download a file' did return a large number of promising looking results. Quote Link to comment https://forums.phpfreaks.com/topic/225252-form-reload-problem/#findComment-1163340 Share on other sites More sharing options...
radi8 Posted January 22, 2011 Author Share Posted January 22, 2011 I will give that a look and try it. This is more of what I am looking for... I can do most anything in PHP, but sometimes get lost these details. You guys are the best. Quote Link to comment https://forums.phpfreaks.com/topic/225252-form-reload-problem/#findComment-1163387 Share on other sites More sharing options...
treeleaf20 Posted January 27, 2011 Share Posted January 27, 2011 So I tested this, here is the code: <script> function check_out(pic_id){ //alert(pic_id); url = "check_out.php?work_id=" + pic_id; window.location.href = url; refreshWorkQueue(pic_id); } </script> The refreshWorkQueue is the AJAX function and the AJAX function is in an external js file. It's weird and the result is not what I expected. The AJAX file actually gives me alerts before the file download is prompted. Also If I comment out the url and the window.location.href and then execute this it actually returns my AJAX file so I'm back to the drawing board. Has anyone else had any luck with this? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/225252-form-reload-problem/#findComment-1165824 Share on other sites More sharing options...
radi8 Posted January 27, 2011 Author Share Posted January 27, 2011 The problem resides in the fact that the php script is still running on the server. While a script is running, only 1 set of headers can be sent. The only way to get something like this to work is to have the current script finish completely and then restart another script to reload the page. But, as of now, I do not know how to flush the current script and start another, possibly by starting a cron job (or something) where the script can complete and do the appropriate garbage cleanup and streams closed, and then start an entirely new script set to reload the page. The key is that the original script must NOT have any active processing at all, it must finish and be flushed/closed completely. Quote Link to comment https://forums.phpfreaks.com/topic/225252-form-reload-problem/#findComment-1165833 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.