title: How to export a excel file using php script
body: <p>I am exporting a excel sheet using php script and my code is</p>
  
  <pre><code>&lt;?php
      function excel_file(){     
      header("Content-Disposition: attachment; filename=\"demo.xls\"");
      header("Content-Type: application/vnd.ms-excel;");
      header("Pragma: no-cache");
      header("Expires: 0");
      $out = fopen("php://output", "w");
      foreach ($tabledata as $data)
      {
          fputcsv($out, $data,"\t");
      }
      fclose($out);
        } 
  ?&gt;
  </code></pre>
  
  <p><strong>HTML</strong></p>
  
  <pre><code>&lt;input type="button" name="excel_file" onclick="&lt;?php excel_file(); ?&gt;" value="Download File"&gt;
  </code></pre>
  
  <p>Problem is this:</p>
  
  <ol>
  <li><p>This <code>function excel_file()</code> execute on loading the page not on click.</p></li>
  <li><p>The excel file which i get, includes the data of whole page not the data of  that perticular array "<code>$tabledata</code>"</p></li>
  </ol>