How To Use Select2 Remote AJAX With Example in JQuery, PHP And MySQL

Many of us are still confused how to implement the select2 JQuery plugin using Remote AJAX. Here I will share how to implement it using Jquery, PHP, and MySql. In addition, to beautify the view, select2 plugin can also facilitate the user in selecting the data displayed on the element select if it has a lot of data. Follow the tutorial select2 using the following ajax to apply to your application.

Surely did you already know “What is select2 JQuery Plugin” ?

Select2 is a jquery plugin to replace the default select box with a more beautiful view. Its use is quite easy, but to display remote ajax data on select2 initially is quite confusing (makes me confused especially). In general, the problem that is often encountered in remote ajax select2 is templateResult: formatRepo and click not working in select2 remote ajax option. From both of these problems, I will try to give the solution in this article.

Tutorial Select2 Using AJAX With Example in Jquery, PHP, and MySQL.

Note: Use Select2 Version 4.0.0 for the application to run properly

How To Use Select2 Remote AJAX With Example In JQuery, PHP And MySQL

1. Prepare the MySql database with “selectdb” name

2. Create a table with country name then fill in the code list and country name as shown below (to speed up the process, please download through my repository at

3. I use PDO as PHP connection method with MySQL. Please create a class for database connections and classes to call MySql tables



4. Create an ajax.php file

5. Create an index.php file in the selectdb project folder and fill in the following code

A brief description:

  • In order to select2 remote ajax to run properly, do not forget to create the format formatRepo and formatRepoSelection.
  • On the server side, json data must have an attribute named “id

To download the source code of the project, please use the following download link. Please share this article using social media button below to open download link.


Hopefully, this tutorial help solves your problem using remote2 ajax select2.

So my tutorials How To Use Select2 Remote AJAX With Example in JQuery, PHP, And MySQL 🙂

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.