Hit enter after type your search item

ajax post method of jQuery to load data in HTML div and table example

Purpose of jQuery $.post method

The $.post method is a shorthand of $.ajax method which is used to load data by an HTTP request.

The $.post method is simpler to use where the data source is specified as follows:

In the url parameter, data source from where data will be pulled is specified. This can be a text file, a PHP file etc. The PHP file may contain a script to connect and retrieve data from a database like MySQL.

You may also send data along with the request, for example, user ID and password etc. The success part is where a callback function executes if the request is successful. There you also get the returned data that can be displayed in different web page elements like div, HTML table, lists, dropdowns etc.

I will show you demos of using jQuery post method in HTML div and table elements. The HTML table will be filled by using database data while for div I will use the text file.

A demo of $.post jQuery method with HTML div

In the demo, a div is created with a little CSS for presentation. As you click on the button “Click here to load data by Post”, the post method will load data from the specified text file.

See the demo and code online by clicking the image or link below:

jQuery post div

See online demo and code

In the markup section, following div and button are created:


Following is the jQuery code used in the <script> section:


You can see, on the click event of button, the post method is called:


The posttst.txt file is placed at the same location where demo source file is located.

The next line of code will display the loaded text to the specified div:


An ajax post example with HTML table

In this demo, as the web page loads, the specified fetchdb.php file is called which is specified in the URL parameter of the $.post method.

The PHP file will connect to MySQL database server and its specified database. After that, an SQL query is used to retrieve data from tbl_products table. The table data is then displayed in an HTML table.

See the demo first followed by a little description:

jQuery post HTML table

See online demo and code

In the script section, the $.post method is called as the demo page loads rather click event of the button, this time.


There, you can see the fetchdb.php file is specified (you can see its code in the demo page). If the request was successful, the returned data in the postresult parameter will be displayed in the body of the web page, by using the $.append method of jQuery.

In the fetchdb.php file, the script is written to connect to the database server. You have to change the user id, password, database name, and table name according to your database specification.

After that, an HTML table is created in that file with a class name:


That CSS class is created in the calling file, where I used the jQuery post method. So it will take the CSS properties to display table data.

At the end, the database connection is also closed.

This div height required for enabling the sticky sidebar