title: PHP export mysql data to excel sheet
body: <p>I have a filter that the user selects or inputs information, and when they hit submit, a query is generated with this code:</p>
  
  <pre><code> &lt;?php 
   function displayrecords(){
     $data1 = $_POST['data1'];
     $data2 = $_POST['data2'];
     $data3 = $_POST['data3'];
     $sql_json = "";
     $where = "";
     if($data1 != ""){
       $where = " `data1` = '".mysql_real_escape_string($data1)."'";
     }
     if($data2 != ""){
       if ( $where != "" ) $where .= " AND ";
       $where = " `data2` = '".mysql_real_escape_string($data2)."'";
     }
     if($data3 != ""){
       if ( $where != "" ) $where .= " AND ";
       $where = " `data3` = '".mysql_real_escape_string($data3)."'";
     }
     if ($where != "") $sql_json = "SELECT * FROM `database`" . " WHERE " . $where . ";";
     $QueryResult = @mysql_query($sql_json) or die (mysql_error());
  
     echo "&lt;table&gt;"
     echo "&lt;tr&gt;&lt;th&gt;Data1&lt;/th&gt;" . "&lt;th&gt;Data2&lt;/th&gt;" . "&lt;th&gt;Data3&lt;/th&gt;&lt;/tr&gt;\n";
     while(($Row = mysql_fetch_assoc($QueryResult)) !== FALSE) {
     echo "&lt;tr&gt;&lt;td&gt;{$Row['data1']}&lt;/td&gt;";
     echo "&lt;td&gt;{$Row['data2']}&lt;/td&gt;";
     echo "&lt;td&gt;{$Row['data3']}&lt;/td&gt;&lt;/tr&gt;\n";
     };
     echo "&lt;/table&gt;"\n";
     }
   }
  </code></pre>
  
  <p>It then spits the data out with this:</p>
  
  <pre><code> &lt;?php displayrecords(); ?&gt;
  </code></pre>
  
  <p>There are no issues here.  The query builder works.  The data is rendered to the screen in the table.</p>
  
  <p>What I need to do now is take the data that has been returned to the screen and export it to an excel sheet using another button in the same form as the other submit button. Here is the button that is in the same form:</p>
  
  <pre><code> &lt;input class="btn btn-success btn-small" type="submit" name="get_report" value="Get Report" /&gt;
  </code></pre>
  
  <p>I don't know if I can use another submit button in the same form. I think on have to use a javascript onclick() function.  I'm not sure.  I just need to export the returned data to excel.</p>
  
  <p>Can anyone help?</p>