Export MySQL Data to Excel in PHP Using AjaxExport MySQL Data to Excel in PHP Using Ajax

Export MySQL Data to Excel in PHP Using Ajax, for doing this task we have to follow 4+ steps where we create a table to show the data and then export that visible MySQL data to an excel sheet.

Export MySQL Data to Excel in PHP Using Ajax
Export MySQL Data to Excel in PHP Using Ajax

Here are the 4+ steps to Export MySQL Data to Excel in PHP Using Ajax,

  • Create a database connection file.
  • Get the complete data using PHP MySQL fetch query.
  • Including Ajax CDN link.
  • Create an Export PHP file which we call using Ajax.
  • Create our main JS file to hit the download.
  • Download complete source code of export to excel using jquery ajax call in PHP.

4 Major files to check the Export in PHP,

  • config.php
  • index.php
  • export.php
  • main.js

Create a Database Connection

We create a MySQL DB connection file where we use mysql_connect() to connect the database.

config.php

Get complete Data from DB

After successfully connected to the database we take the user’s data on our web view using MySQL select query and run that query using PHP mysqli_query() function.

Include Ajax CDN link and Show Data

Here we create our main view to show the data which we get from the select query. And that complete data we export on Excel.

index.php

Here is a trick to doing it completely, without a refresh or reload the page,

On the above code snippet, we create a link on the href attribute, and then we hit using our ajax file (main.js) to download the file without open new tab and without refresh the page.

Create an Export PHP file

export.php

Code Explanations:

  • First, we include the DB connection file.
  • Check the button is clicked or not using isset() PHP function.
  • Then we set the mandatory headers for the create Excel supported files.
  • Now we setting the header of CSV file.
  • Now we take the complete data using SQL query and put that into the file using fputcsv($output, $row); function.

Create Ajax File to Call Export Function

Here we create the main JS file which we use to call the export.php file where we write the export or download file code.

main.js

Code Explanations:

  • First, we statically assign the value to a variable (you can create it dynamic by getting the button value using ID). We send it to the PHP isset() function to check button is clicked or not.
  • Then we get the result on success response.
  • On HTML we discuss the URL trick, for that, we take <a> tag by ID and create href link using JS BLOB.
  • And auto click on that button which we create inside that <a> tag to hit the download link.
  • Complete process on setTimout() function because link create may take some second to appear.

Here is the complete source code and its explanations to create Export MySQL Data to Excel in PHP Using Ajax, you check the demo on the below video.

Download Complete Source Code of Export MySQL Data to Excel in PHP Using Ajax

https://codingtasks.net/download/download-complete-source-code-of-export-mysql-data-to-excel-in-php-using-ajax/

Also Check:

Happy Coding..!

Was this article helpful?
YesNo

By Bikash

My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.

5 thoughts on “Export MySQL Data to Excel in PHP Using Ajax”

Leave a Reply

Your email address will not be published. Required fields are marked *