title: $_POST data to phpExcel
body: <p>I am trying to export data from a html table to an excel file using phpExcel. The issue I am having is that I cannot seem to get the data through to it. </p>
  
  <p>I have the following jquery code which runs on a button press to get the data out of the table and post it to the php code.</p>
  
  <p>export data jquery</p>
  
  <pre><code>function storeTblValues()
  {
  var tableData = new Array();
  
  $('#LogsTable tr').each(function(row, tr) 
  {
      if(row == 0) //Table Headers
      {
          tableData[row] = 
          {            
              "LogDate" : $(tr).find('th:eq(0)').text(),
              "LogType" : $(tr).find('th:eq(1)').text(),
              "StartTime" : $(tr).find('th:eq(2)').text(),
              "FinishTime" : $(tr).find('th:eq(3)').text(),
              "Duration" : $(tr).find('th:eq(4)').text()
          } 
      }
      else //Table data
      {
          tableData[row] = 
          {  
          "LogDate" : $(tr).find('td:eq(0)').text(),
          "LogType" : $(tr).find('td:eq(1)').text(),
          "StartTime" : $(tr).find('td:eq(2)').text(),
          "FinishTime" : $(tr).find('td:eq(3)').text(),
          "Duration" : $(tr).find('td:eq(4)').text(),
          }            
      }
  });
  return tableData;
  }
  
  function exportToExcel()
  {      
      var tableData;        
      tableData = $.toJSON(storeTblValues());
      var tmp = "pTableData=" + tableData;   
  
      $.ajax({
          type: 'POST',
          url: 'create_export_files.php',
          data : tmp,    
          success : function(data)
          {
              window.location = 'create_export_files.php';        
          }
      });    
  }
  </code></pre>
  
  <p>Top bit of the php file receiving the data
  Create_export_files.php</p>
  
  <pre><code>$tableData = "";
  
  if (isset($_POST["pTableData"]))
  {
      $tableData = $_POST["pTableData"];
      $tableData = json_decode($tableData, TRUE);
  }
  else 
  {
      $tableData = "empty";
  }
  
  
  generateExcel($tableData);
  
  function generateExcel($tableData)
  {
  
      /** Include PHPExcel */
      require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
      // Create new PHPExcel object
      $objPHPExcel = new PHPExcel();
          // Add some data
  $objPHPExcel-&gt;setActiveSheetIndex(0)
              -&gt;setCellValue('A1', 'hello')
              -&gt;setCellValue('B2', 'world!')
              -&gt;setCellValue('C1', 'Hello')
              -&gt;setCellValue('D2', 'world!');
  
  // Rename worksheet
  $objPHPExcel-&gt;getActiveSheet()-&gt;setTitle('Simple');
  
  
  // Set active sheet index to the first sheet, so Excel opens this as the    first sheet
  $objPHPExcel-&gt;setActiveSheetIndex(0);
  $filename = 'export '.date('d-m-Y h:i:a').'.xlsx';
  
  
  // Redirect output to a client’s web browser (Excel2007)
  header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  header('Content-Disposition: attachment;filename="'.$filename.'"');
  header('Cache-Control: max-age=0');
  // If you're serving to IE 9, then the following may be needed
  header('Cache-Control: max-age=1');
  
  // If you're serving to IE over SSL, then the following may be needed
  header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
  header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  header ('Pragma: public'); // HTTP/1.0
  
  $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  $objWriter-&gt;save('php://output');
  }
  </code></pre>
  
  <p>I always seem to be going into the else part of the statement. Where am I going wrong?</p>
  
  <p>If I remove the isset bit around the pTableData then the file gets created but is corrupted. Opening it in notepad++ I can see it says pTableData is undefined. Is that because the php file gets run again to make the file download?</p>