Skip to content
Advertisement

php mysql export excel

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?

JavaScript

Advertisement

Answer

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:

JavaScript

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;

JavaScript

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

JavaScript

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:

JavaScript

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.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement