Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9303285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:26:59+00:00 2026-06-18T23:26:59+00:00

This code is working without problem if page doesn’t contain any other content. When

  • 0

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?

<?php
if (isset($_POST['excel'])){
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());
mysql_query("SET NAMES 'LATIN5'");
mysql_query("SET character_set_connection = 'LATIN5'");
mysql_query("SET character_set_client = 'LATIN5'");
mysql_query("SET character_set_results = 'LATIN5'");

$select = "query here";

$export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) );


$fields = mysql_num_fields ( $export );

for ( $i = 0; $i < $fields; $i++ )
{
    $header .= mysql_field_name( $export , $i ) . "\t";
}

while( $row = mysql_fetch_row( $export ) )
{
    $line = '';
    foreach( $row as $value )
    {                                            
        if ( ( !isset( $value ) ) || ( $value == "" ) )
        {
            $value = "\t";
        }
        else
        {
            $value = str_replace( '"' , '""' , $value );
            $value = '"' . $value . '"' . "\t";
        }
        $line .= $value;
    }
    $data .= trim( $line ) . "\n";
}
$data = str_replace( "\r" , "" , $data );

if ( $data == "" )
{
    $data = "\n(0) Records Found!\n";                        
}

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=your_desired_name.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data"; }

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T23:27:00+00:00Added an answer on June 18, 2026 at 11:27 pm

    As mentioned in the comments, combining HTML and CSV is not possible

    However, it seems you’re only outputing CSV if a form is posted ($_POST[‘excel’]). You should ‘stop’ the PHP script after outputting the CSV, like this:

    if($_POST['excel']) {
        // query and output CSV 
        //...
    
    
        exit(); //done outputting csv
    }
    
    // regular HTML form here; will only be output if NO form is sent
    

    With this approach, your script will:

    • output a HTML form if visited without sending a form
    • output CSV if the form has been submitted.

    However, you may re-consider putting both functionality in the same PHP script. It’s probably a better approach to output the CSV in another script (e.g. download_csv.php) and set the ‘action’ of your form to that location;

    <form action='download_csv.php'>
    

    [update]
    To output HTML or CSV depending on the form-input, you may consider this:

    if($_POST['excel']) {
        // query and output CSV 
        include 'csv-exporter.php';
        exit(); //done outputting csv
    } else if ($_POST['html']) {
        // query and output HTML
        include 'html-exporter.php';
        exit(); //done outputting html
    }
    

    However, this way the ‘query’ part will be duplicated. In which case, it’s probably best to separate the querying and ‘formatting’ parts. You can do so by putting them in a function and use the result of that, something like:

    if($_POST['excel']) {
        outputCSV(getResultsFromDatabase());
        exit();
    } else if ($_POST['html']{
        outputHtmlList(getResultsFromDatabase());
        exit();
    }
    

    Those functions can be put in separate files and included using the ‘include’ functionality of PHP.

    Both are just for illustration all purposes, and a bit off-topic for your original question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code is working on other test environment but not on mine. Do you
I have this test page I am working from: http://www.problemio.com/test.php The problem happens when
I have this code working in C#: var request = (HttpWebRequest)WebRequest.Create(https://x.com/service); request.Method = GET;
Why isn't this code working? I've been stuck on this for 2 days. public
I used to have this code working with my Tomcat server: HttpRequestBase targetRequest =
i got this code working and it to return the last tweet from my
I tried everything to get this code working, and I hope someone will save
Simple question, how make this code working ? public class T { public static
this seems to be weird but it really happened. I have this code working
This code was working earlier today, then randomly stopped, something must of changed, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.