Helpp with strange form behavior

Good day all:
I have a small script that will allow users to select a database table and extract the data into an excel file. This all works well, but the page acts relatively strange after the file has been downloaded.

What is happening is that after the file is retrieved, the page does not refresh, and in Firefox, if I select another file to extract, it will draw unformatted plain html as text to the screen.

It would appear as though the export to excel is creating the excel file and also the download header correctly, but it is not redrawing the form.html.php page(???).

I think that what I need is a way to force the download page to process, and then flush the header so that the form.html.php can fully reload (see the code index.php below and also the header details from the excel process). I did a test on the page to see if a control element is being reset changed after the file download process. I created a variable in the controller and in the form page, if the value is a 1, I display a message, otherwise I do not. Initially I set it to display the message (first load), then I processed the excel file and reset the variable, but the message persisted. So, the page is not being reprocessed after the excel process.

So, what I need to find is a way to force the page to reload completely after the Excel file has been processed. FYI: I have the PHP.INI file setting: Output_Buffering to ‘ON’.

I hope this makes sense to you, if not, I can try to clarify as best I can and/or supply any other code needed.

PHP Code:


<?php
  
  include_once $_SERVER['DOCUMENT_ROOT'] .
        '\\inc\\magicquotes.inc.php';
  include_once $_SERVER['DOCUMENT_ROOT'] .
        '/inc/base.inc.php';
  include_once $_SERVER['DOCUMENT_ROOT'] .
        '/admin/export/excel-xml.php';
  $frmErrorLevel=0;
  $frmErrMsg='';
  
  if(isset($_POST['action']) && $_POST['action']=='submitted')
  {
      $carrierRatesdisabled=true;
        
      if(isset($_POST['ExportCarrier']))
      {
          exportTruckingCompanies();
      }
      if(isset($_POST['ExportDropFees']))
      {
          exportDropFees();
      }
      if(isset($_POST['ExportDomesticRates']))
      {
          if(isset($_POST['Carrier_Name']) && $_POST['Carrier_Name'] == "0")
          {
              $frmErrorLevel=3;
          }
          else
          {
            exportDomesticRates($_POST[Carrier_Name]);
          }
      }
      if(isset($_POST['ExportCanadianRates']))
      {
          exportCanadianRates();
      }
      if(isset($_POST['ExportFuelSurcharge']))
      {
          exportFuelCurchargeRates();
      }
  }
  else
  {
      $carrierRatesdisabled=true;
  }
  
  // get the carrier recordset for the drop list
  
   $sql = 'SELECT * FROM `Carriers` order by `CarrierName`';
   openRecordset($sql,$result, $link);
    if($result)
    {
        while ($row = mysqli_fetch_array($result))
        {
            $carrierNames[] = array('id' => $row[0], 'name' => html($row[1]));
        }
         mysqli_free_result($result);
         close_mysql($link);
    }
    else $frmErrorLevel=1;
    if (ob_get_length()){           
        @ob_flush();
        @flush();
        @ob_end_flush();
    } 
    include 'form.html.php';
  exit();
?>

Excel output header information:


 function save($filename, $download=false, $download_filename="")
    {
        if (!$download)
        {
            return $this->domXML->save($filename);
        }
        elseif ($this->domXML->save($filename))
        {
            $FileInfo = pathinfo(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.
            if ($download_filename == "")
                $download_filename = "download.xls";
            header("Content-Disposition: attachment; filename=".$download_filename.";"); 
            header("Content-Transfer-Encoding: binary"); 
            header("Content-Length: ".filesize($filename)); 
            @readfile($filename);
            return true;
        }
        return false;
    }

On my localhost, the php.ini had output_buffering turned on. I turned it off and now the plain text HTML does not show, but the page does not redraw itself after the Excel download.

I need to have this page refresh so that I can show the status information.

You are basically trying to have the server respond with both an excel file and a html page in one request. Just not going to work. One or the other but not both.

What you could do is:

  1. POST the page
  2. Generate excel file storing it on server, store status in session
  3. Redirect and show the status with a link to the stored excel file
  4. The user reviews the status then clicks on the link to see the file
    OR
    Use ajax/js