Hit enter after type your search item

AJAX / Post in jQuery to load data from text / MySQL in Bootstrap tabs

By using jQuery ajax and post methods, you can load data in different HTML components like tables, lists, dropdowns etc. You may also load data in Bootstrap tabs by using the $.ajax or $.post method of jQuery.

In this tutorial, I will show you demos of using these methods where data will be loaded from a text file, as well as, by using a PHP file that connects to a MySQL database to retrieve data from its table.

The table data will be shown in a tab of Bootstrap without reloading the web page.

A demo to load data from a text file

In this demo, first I have created Bootstrap based tabs by using its built-in plug-in. Four tabs are created by built-in classes that are initiated as web page loads.

The fourth tab, “jQuery” is filled by using the jQuery ajax method. See this demo online and I will explain what code is used to accomplish that.

Bootstrap tabs ajax

See online demo and code

You can see, the demo page shows jQuery tab opened as web page loads. I have kept this tab as “active” by using the .active class.

I assume you already have an idea about how to create Bootstrap tabs. The focus is loading data by using $.ajax call. For that, look at the “jQuery” tab’s tag.

Its tab is created just like three other tabs:


 

Whereas, if you look at the tab pane’s area, this code is used:


 

Still no difference, except no text or content is given there, unlike all three other tabs. Now look at the <script> section in head tag:


 

This is where the div of “jQuery” tab is called after using the $.ajax jQuery method. The ajax method loads data from the given text file which (if successful) will be assigned or appended to the “#jquerytab”, which is the id of jQuery tab.

You may see and get the complete code in the demo page along with seeing the output.

A demo of $.post method to load data in Bootstrap tabs by PHP/MySQL

In this demo, I have used $.post jQuery method, rather than $.ajax. Also, instead of a text file, I have loaded data from the MySQL database by using the PHP script.

The script is called in the $.post method. Have a look at the online demo:

Bootstrap tabs post

See online demo and code

Consider following code in the <script> section of <head> tag:


 

The jQuery post method will execute as you click the button “Load Products Data” unlike the above example where it loaded data as web page opened. I used this to show the data loading without refreshing the web page by using ajax’s post method of jQuery.

The line:

$.post(“fetchtab.php”,function(postresult){

calls the fetchtab.php file, that connects to the MySQL database server and retrieves the products information from the specified table in SQL query. The file also returns the HTML table with DB table’s data as a response to the request in $.post method.

The postresult will get the returned information (if successful) and is assigned to products pane of Bootstrap tabs.


 

In the markup section, the tabs are created just like the above example:


 

You can also see the <style> tag in the <head> section that contains the style for tabs which overrides the Bootstrap classes for tabs. Along with it, classes for displaying HTML table can also be seen there.

Feel free to change styles, if you liked these demos!

This div height required for enabling the sticky sidebar