jQuery AJAX tutorial: 7 demos of div, form, tabs, accordion, and table

Purpose of jQuery AJAX method

The jQuery has handy methods to work with AJAX quite simply and easily. The methods include: $.ajax, $.post, $.get, $.load etc.

By using these methods, you may accomplish certain tasks like Submitting forms without reloading a web page, pulling data in an autocomplete field, loading table data, using tabs which data is based at AJAX calls and so on.

I will show you demos of some useful components using AJAX jQuery method; first have a look at the basic syntax:

 

The parameters are optional in $.ajax method. This is where you may specify the data source by giving a URL, any data sending to the server (e.g. userid, password), dataType expected in response, error handling and others. For example:

 

And using data parameter

 

}});

A few useful parameters are also used in the demos with some explanation wherever required. If you are a beginner to this then go through this example. If you have some idea, then move to more advanced demos below.

A basic example to use $.ajax method

In this simple example, a text file is used to explain how jQuery ajax method works. Only a few parameters are used:

  • A text file is specified in the url parameter.
  • If the request is successful, it will assign data to the string.
  • By using the $.append method of jQuery, the returned data will be displayed in a div.

See the demo online first:

jQuery ajax div

See online demo and code

In the click event of the button, I used the $.ajax method of jQuery. There, I specified the text file in the URL parameter. If the request is successful, a callback function runs that also contains the result (which is text file data in that case).

Finally, I simply displayed the returned data in an existing div element by using the append method. The jQuery code is:

 

An ajax example with jQuery accordion

The following example is a little advanced that uses AJAX in a jQuery UI accordion. For that, an accordion with three panels is created by using the jQuery UI library.

The information in the content area is pulled from the text files by using the jQuery $.ajax method for the first two panels. See this demo online:

jQuery ajax accordion

See online demo and code

You can see, the first panel is showing some plain text with list items. Inside the $(document).ready function, two ajax calls are made to load data for first two panels.

For the first panel:

 

Where accord1.txt file is at the same location where the source file is placed. The #accord1 refers to the <p> tag that is used for the content area of the accordion.

For the second panel following jQuery code is used:

 

The list items are included just for the demonstration that text files may also include HTML tags.

The third panel’s content area is pre-defined in the markup.

In other scenarios, you may load data from some database. See an example below where I used PHP/MySQL to display data in the HTML table.

You may make the ajax call as per user’s action like clicking a link or button rather than loading as web page loads.

An ajax jQuery example with tabs

Just like accordion’s data, you may also load data in the tabs of a webpage. The tabs are a good way for presenting information if you have limited space with lengthy information. Or it can be used for distinguishing different categories.

For example, in a mobiles website, you may present product information in one tab, and user reviews on other.

For the demo, I have created three tabs by using the jQuery UI plug-in.

Although, this is a built-in feature while using the jQuery tabs to pull data where almost all work is done to make a call to text, PHP or some other files.

I will also show you using the $.ajax method like I used for accordion.

First, have a look at pulling data by built-in feature, that also uses AJAX:

jQuery ajax tabs

See online demo and code

You can see in the <a> tags of each tab, a link to the text file is specified that uses underlying built-in support to pull data by using AJAX.

 

You may also specify a PHP, ASP.Net or some other script file there that connects to the database server to fetch data.

By using $.ajax method:

The same example as above, except some color scheme is changed by overriding the default properties of jQuery UI tabs. In this demo, I have used the same approach as used for the accordion. See the demo online first with code:

jQuery ajax tabs corss

See online demo and code

The same data is loaded in the tabs by calling it in the script section:

 

Again, you may specify the PHP or some other script to connect with the database server for displaying DB driven data in the tabs by using jQuery AJAX method.

An example to fetch data from MySQL database by using PHP

In the following example, a PHP file is used in the $.ajax method. The PHP file connects to the server and fetches the data from a MySQL database table. The PHP file also contains the markup for the HTML table. See the demo below which is followed by a little detail:

jQuery ajax-PHP database

See online demo and code

First of all, let us have a look at the PHP file code that communicates with the database.

Step1:

A test database is created in the MySQL database server for this demo. As this PHP file is called by using the $.ajax method it connects to the database and its specified table (tbl_products):

 

 

The next statement is to execute the SQL query to fetch table data:

 

 

Step 2:

The final step in the PHP file is to write the markup for the HTML table. where a CSS class name is also given.

 

That’s it, as for as PHP file is concerned. The CSS style is contained in the head section of calling file where I used the $.ajax method.

In the calling file, this jQuery code is used at the click event of the button:

 

So, it will display the returned data in the body selector. You may use other selectors like a div, paragraph etc.

In the demo page, you can see the demotbl CSS class style which is used for the table. Along with it, you can see the complete code for this demo there (for both files).

A jQuery ajax example with Bootstrap form and PHP

The following demo is a combination of Bootstrap form controls with jQuery ajax method. It will also use a simple PHP script.

As you fill the form fields in the Bootstrap form and press the Create Account button, the $.ajax method will be called on the click event of that button. In the ajax jQuery method, the post_ajax.php file is called along with sending form fields data. It will simply return the form data that will be displayed in a div element.

See the demo first:

jQuery ajax form

See online demo and code

Let me go step by step to explain this Bootstrap form with $.ajax:

In the HTML page

Step 1:

The Bootstrap framework’s CSS is included in the head section along with the jQuery library.

Step 2:

A Bootstrap form is created by using the built-in Bootstrap classes.

 

Step 3:

A Bootstrap button is placed to call the $.ajax method:

 

Step 4:

Following jQuery code is used to call $.ajax method where the PHP file is called:

 

In the post_ajax.php file, following code is written:

 

As you click the button, the $.ajax method will be called. In the url parameter, the post_ajax.php file is used.

In the data parameter, the form field’s data is sent to the server that will be picked up by that PHP file.

In the PHP file, I simply display the form data by using echo statement, after fetching it to PHP variables.

In other scenarios, you may want to save this data to the database to create an account and displaying the success message, as a response.

Using $.post method to pull data rather $.ajax

The $.ajax method of jQuery has a few high level methods that use $.ajax underneath. These methods include $.post, $.get, $.load etc. Generally, these high level methods are easier to use.

The following is a replica of the first example where I pulled data in a div element. The only difference is, I have used the $.post method this time instead of $ajax.

jQuery ajax post

See online demo and code

This is the jQuery code:

 

Similarly, you can use the $.get method if you want to work with cached data. The $get also uses $.ajax underway. Where to use $.get and post are explained in their own chapters with demos.