title: php mysql export excel
body: <p>This code is working without problem if page doesn't contain any other content. When i add a html form, it gives only html content to excel. I need to use that form to customize query. How can i make this code working with html content?</p>
  <pre><code>&lt;?php
  if (isset($_POST['excel'])){
  mysql_connect(&quot;localhost&quot;, &quot;root&quot;, &quot;&quot;) or die(mysql_error());
  mysql_select_db(&quot;dbname&quot;) or die(mysql_error());
  mysql_query(&quot;SET NAMES 'LATIN5'&quot;);
  mysql_query(&quot;SET character_set_connection = 'LATIN5'&quot;);
  mysql_query(&quot;SET character_set_client = 'LATIN5'&quot;);
  mysql_query(&quot;SET character_set_results = 'LATIN5'&quot;);
  
  $select = &quot;query here&quot;;
  
  $export = mysql_query ( $select ) or die ( &quot;Sql error : &quot; . mysql_error( ) );
  
  
  $fields = mysql_num_fields ( $export );
  
  for ( $i = 0; $i &lt; $fields; $i++ )
  {
      $header .= mysql_field_name( $export , $i ) . &quot;\t&quot;;
  }
  
  while( $row = mysql_fetch_row( $export ) )
  {
      $line = '';
      foreach( $row as $value )
      {                                            
          if ( ( !isset( $value ) ) || ( $value == &quot;&quot; ) )
          {
              $value = &quot;\t&quot;;
          }
          else
          {
              $value = str_replace( '&quot;' , '&quot;&quot;' , $value );
              $value = '&quot;' . $value . '&quot;' . &quot;\t&quot;;
          }
          $line .= $value;
      }
      $data .= trim( $line ) . &quot;\n&quot;;
  }
  $data = str_replace( &quot;\r&quot; , &quot;&quot; , $data );
  
  if ( $data == &quot;&quot; )
  {
      $data = &quot;\n(0) Records Found!\n&quot;;                        
  }
  
  header(&quot;Content-type: application/octet-stream&quot;);
  header(&quot;Content-Disposition: attachment; filename=your_desired_name.xls&quot;);
  header(&quot;Pragma: no-cache&quot;);
  header(&quot;Expires: 0&quot;);
  print &quot;$header\n$data&quot;; }
  
  ?&gt;
  </code></pre>