AJAX Autocomplete Example
July 9th, 2009
In this video, I show how to build a simple autocomplete search box. You have probably seen this feature on popular sites such as Netflix, Google, Hulu and Adobe. When you start to type in a search box, you get results returned immediately that match what you typed.
I used PHP, MySQL and jQuery to create this example.
What you need to do in order to get this to work:
In order to get this working on your site, you will need to follow a few steps.
- Setup your database. You can use a database you already have or create a new one. If you already have a database and table created you can skip this step. You can view my other MySQL tutorials for more details on setting up a database.
- Modify the include/dbconn.php file. You will need to modify this file to connect to your database. You will need to provide your server name, username, password and database name.
- Possibly make changes to get_customers.php. You will most likely have to make some changes to this file as well in order to use your own table name.
For example, on line 9 my query looks like this:
$query = "SELECT first_name, last_name FROM Customers WHERE last_name LIKE '%" . $queryString . "%' ORDER BY last_name, first_name";
You will have to change that line of code to match your database table and column names.
You will also need to modify this line of code from get_customers.php:
echo '<li onclick="fill(\'' . $row['first_name'] . ' ' . $row['last_name'] . '\');">' . $row['first_name'] . ' ' . $row['last_name'] . '</li>';
You will need to change $row['first_name'] and $row['last_name'] to match the name of your columns.