Jump to content

Form Reload problem


radi8

Recommended Posts

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 "&nbsp"; 
                    ?>
                </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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.