title: Exporting data to excel with PHP
body: <p>I'm making a simple form that asks for 2 dates , when inputted by the user and submitted a table will appear with the data in that date range. -&gt; this works</p>
  <p>code :</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang=&quot;en&quot;&gt;
  
  &lt;head&gt;
      &lt;meta charset=&quot;UTF-8&quot;&gt;
      &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
      &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
      &lt;title&gt;SWI report&lt;/title&gt;
      &lt;style&gt;
          table {
              font-family: Arial, Helvetica, sans-serif;
              border-collapse: collapse;
              width: 100%;
          }
  
          td,
          th {
              border: 1px solid #ddd;
              padding: 8px;
          }
  
          tr:nth-child(even) {
              background-color: #f2f2f2;
          }
  
          tr:hover {
              background-color: #ddd;
          }
  
          th {
              padding-top: 12px;
              padding-bottom: 12px;
              text-align: left;
              background-color: #4CAF50;
              color: white;
          }
      &lt;/style&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
      &lt;form method=&quot;post&quot; action=&quot;&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;&quot;&gt;
          startdate: &lt;input type=&quot;date&quot; name=&quot;from_date&quot;&gt;
          enddate: &lt;input type=&quot;date&quot; name=&quot;to_date&quot;&gt;
          &lt;input type=&quot;submit&quot; name=&quot;date&quot; id=&quot;date&quot;&gt;
      &lt;/form&gt;
  
      &lt;div&gt;
          &lt;!-- excel export !--&gt;
          &lt;form action=&quot;&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;&quot; method=&quot;POST&quot;&gt;
              &lt;button type=&quot;submit&quot; name=&quot;excel&quot; value=&quot;excel&quot; id='excel'&gt; Export to excel&lt;/button&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  
  
      &lt;?php
      require('settings.php');
      mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
  
      if (isset($_POST[&quot;date&quot;])) {
          $startDate = date(&quot;Y-m-d&quot;, strtotime($_POST['from_date']));
          $endDate = date(&quot;Y-m-d&quot;, strtotime($_POST['to_date']));
  
  
          $sql = &quot;SELECT latestv.* from(
          select distinct Werkomschrijving_nr from POH_GL4 where versie Between ? and ? ) changedw
          left join
          (select Werkomschrijving_nr, max(versie) AS maxdate, omschrijving from POH_GL4 
          group by Werkomschrijving_nr,omschrijving) latestv on latestv.Werkomschrijving_nr = changedw.Werkomschrijving_nr&quot;;
  
          $stmt = $db-&gt;prepare($sql);
          $stmt-&gt;execute([$startDate, $endDate]);
          $result = $stmt-&gt;fetchAll();
  
          echo &quot;&lt;table&gt;&quot;;
          echo &quot;&lt;tr&gt;&lt;th&gt;nr werkomschrijving&lt;/th&gt;&lt;th&gt;Last change date &lt;/th&gt;&lt;th&gt;Omschrijving&lt;/th&gt;&lt;/tr&gt;&quot;;
  
          foreach ($result as $key =&gt; $row) {
  
              echo &quot;&lt;tr&gt;&quot;;
              echo &quot;&lt;td&gt;&quot; . $row['Werkomschrijving_nr'] . &quot;&lt;/td&gt;&quot;;
              echo &quot;&lt;td&gt;&quot; . $row['maxdate'] . &quot;&lt;/td&gt;&quot;;
              echo &quot;&lt;td&gt;&quot; . $row['omschrijving'] . &quot;&lt;/td&gt;&quot;;
              echo &quot;&lt;/tr&gt;&quot;;
          }
  
          echo &quot;&lt;/table&gt;&quot;;
  
  
          if (!empty($_POST)) {
              if (empty($result)) {
                  echo '&lt;/br&gt;';
                  echo 'no results for this timeframe';
              }
          }
      
  
      }
  
      ?&gt;
  
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  <p>I want to export the data in the table to excel but I cant seem to make it work. I tried putting the table data in a session but when I try to click on the export to excel button it looks like it just wipes the data. can someone help me on this one ?</p>
  <p>kind regards</p>