Hit enter after type your search item

jQuery: Post method to Load text file and database data in accordion

The jQuery post method

In this tutorial of jQuery post function, I will show you how to load data in the jQuery UI accordion by using shorthand $.ajax method of jQuery i.e. $.post.

This would be the output of the example along with the complete code. You can see steps to create after that.

jQuery post accordion text

See online demo and code

First of all, an accordion is created by including the jQuery UI libraries in the <head> section.

This includes both CSS and JS library:

<link rel=”stylesheet” href=”//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css”>

<script src=”//code.jquery.com/ui/1.11.2/jquery-ui.js”></script>

In the markup section, a div element is created that will act as the accordion. Accordion panels are created by using <h3> heading and <p> tag inside that div element:


The Script part

In the script section under the head tag, the accordion is initiated by calling that div element.


After that, the jQuery post method is called that is loading the text file data:


The post1.txt and post2.txt files are placed at the same location where example HTML file is placed. Change the location according to your directory structure.

The posresult contains the returned data after using the $.post method. This is assigned to <p> tags by using $.append method.

The CSS part

After the script section, the <style> is used for the accordion. There, I overrode the default classes that are used in the jQuery UI CSS.

For example, ui-accordion .ui-accordion-header is used to override default colors for the headers of the accordion.

You can see the complete code in the demo page along with the output.

A Post function example to load data from database

In this example, I will use the same accordion with slight changes in color scheme. However, this time, the data is pulled from the MySQL database’s table rather than a text file.

In the first panel, a PHP file is called in the $.post jQuery function that contains the script to connect with the database which is followed by retrieving data from a table.

The table is displayed in the first panel. See the code and output which is followed by a little description.

jQuery post accordion database

See online demo and code

In the <script> section, I used postdb.php file in the first panel’s $.post method that connects and retrieves the data from MySQL database:


The complete script is placed just before the </body> tag this time:


While CSS properties are changed a bit including the content area for this example:

See the complete code including PHP code to in the demo page.

If you want to try this at your local or remote machine, simply change the parameters of DB user ID and password:


Also, change the DB name from testdb to one that you intend to use. Change the SQL query according to your database’s table as well.

This div height required for enabling the sticky sidebar