title: Download HTML table into excel using PHP
body: <p>I have a table on my webpage that gets populated with data based on a persons callsign. Once the data is in the table, I have a download button to download the table into an excel document. Right now when you click the button it downloads and excel file but my table is not in there. How would I populate the excel file with my table? Any thoughts or ideas are greatly appreciated! </p>
  
  <pre><code>&lt;?php foreach ( $station_array as $station ) {
              $name = $station[ 'name' ];
              $temp = $station[ 'temp' ];
              $time = $station['time'];   
              $temp1 = ( ( 9 / 5 ) * $temp ) + 32;        
              echo '&lt;table border = "1"  name = "table" class = "Choices"&gt;    
              &lt;tr&gt;
                  &lt;th&gt;NO.&lt;/th&gt;
                  &lt;th&gt;Fields&lt;/th&gt;
                  &lt;th&gt;Data&lt;/th&gt;
              &lt;/tr&gt;
                  &lt;tr&gt;&lt;td&gt;1&lt;/td&gt;
                  &lt;td&gt;Callsign&lt;/td&gt;
                  &lt;td&gt;'.$name.'&lt;/td&gt;                  
                  &lt;/tr&gt;
                  &lt;tr&gt;
                      &lt;td&gt;2&lt;/td&gt;
                      &lt;td&gt;Time&lt;/td&gt;
                      &lt;td&gt;'.$time.'&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                  &lt;td&gt;3&lt;/td&gt;
                  &lt;td&gt;Temperature&lt;/td&gt;
                  &lt;td&gt;'.$temp1.'&lt;/td&gt;
                 &lt;/tr&gt;&lt;/tabe&gt;';?&gt; 
  </code></pre>
  
  <p><code>&lt;form class = "downLoadButton" action ="downLoad.php" method="post"&gt;
          &lt;input type="submit" name = "submit" value="Download Data" /&gt;
          &lt;/form&gt;</code></p>
  
  <p>DownLoad.php</p>
  
  <pre><code>&lt;?php
    $filename = "aprsData_" . date('Ymd') . ".xls";
    header("Content-Disposition: attachment; filename=\"$filename\"");
    header("Content-Type: application/vnd.ms-excel");
  
    if (isset($_POST["submit"])) {
      $data .= $_POST["table"];
      }
  
   $flag = false;
    foreach($data as $row) {
      if(!$flag) {
        // display field/column names as first row
        echo implode("\t", array_keys($row)) . "\n";
        $flag = true;
      }
      echo implode("\t", array_values($row)) . "\n";
    }
  ?&gt;`
  </code></pre>