title: Export query from user input to Excel - only HTML parts are visible
body: <p>What I'm trying to do here is to let the user input DATEFROM and DATETO. Then when the user click the button extract to Excel file, the query will be executed where it will select all columns from the table where <code>BETWEEN DATEFROM and DATETO</code>. At the same time, it will download an excel file where the output should be there. </p>
  
  <p>I was able to get the right query and output it in the html but I'm having problems in exporting it to Excel. Here are the problems:</p>
  
  <ol>
  <li><p>Exporting to Excel is working even though I don't click the button. When I run the program in localhost, it automatically export to Excel file.</p></li>
  <li><p>What is inside the Excel file are only the HTML part only.</p>
  
  <p>
      
      </p>
  
  <pre><code>    &lt;form method="POST" action=""&gt;
      DATE FROM: &lt;input type="date" name="datefrom"&gt; TO: &lt;input type="date" name="dateto"&gt; &lt;input type="submit" value="Extract excel file" name="extract"&gt;&lt;/input&gt;
      &lt;/form&gt;
  
      &lt;?php   
      header("Content-type: text/csv; charset=UTF-8");
      header('Content-Disposition: attachment; filename=Export.csv');
  
      if(isset($_POST['extract'])){
          $con = mysql_connect("localhost", "root");
          if(!$con){
              echo "Error connection";
                  }
  
          $select_db = mysql_select_db('sample', $con);
          if(!$select_db){
              echo "Error to select database";
                  }
          mysql_set_charset("utf8", $con);
  
        $datefrom = $_POST['datefrom'];
        $dateto = $_POST['dateto'];
  
      $myquery = mysql_query("SELECT * FROM biometrics WHERE date_created BETWEEN '" . $datefrom . "' AND '" . $dateto . "' ORDER BY empno");
  
      //While loop to fetch the records
      $contents = "id_biometrics,empno,date_created,time_created,status\n";
      while($row = mysql_fetch_array($myquery))
      {
          $contents.=$row['id_biometrics'].",";
          $contents.=$row['empno'].",";
          $contents.=$row['date_created'].",";
          $contents.=$row['time_created'].",";
          $contents.=$row['status']."\n";
  
      }
  
      $contents_final = chr(255).chr(254).mb_convert_encoding($contents, "UTF-16LE","UTF-8");
      print $contents_final;
      }
      ?&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p></p></li>
  </ol>