When you start typing in a website’s search box, and instant suggestions appear, that’s a live search in action. A live search box (often called AJAX search or autocomplete) dynamically fetches results from the server as you type without requiring a full page reload. This makes searching faster, more interactive, and more user-friendly. Instead of submitting a form and waiting for a new page, the browser sends a background request (via AJAX) on each keystroke and updates the results on the fly. This immediate feedback loop enhances the user experience and keeps visitors engaged with your site.
In this tutorial, we’ll walk through creating a live search feature using PHP (for server-side logic), MySQL (to store and query data), and AJAX with a bit of JavaScript (to handle the dynamic request and display). By the end, you’ll have a working search box that suggests results instantly as the user types.
Today’s users expect speed and interactivity. A live search implementation fulfills these expectations by delivering instant results. Here are a few benefits of adding a live search box to your website:
In short, a well-implemented live search enhances usability and can make your website feel much more dynamic and intelligent.
Before we begin, make sure you have the following in place:
Note: If you haven’t installed a local server environment yet, download and install XAMPP or a similar solution. Start the Apache and MySQL services through the control panel before proceeding.
Once you have the environment ready, it’s time to start building. We will first set up a database with sample data, then create the front-end interface and backend scripts for the live search. After testing locally, you can deploy your application to a live server. Platforms like Voxfor offer reliable PHP/MySQL hosting, making it easy to host your application and serve your new live search feature to users.

First, let’s prepare the database that our live search will query. We need to create a MySQL database and a table with some sample data to search through.
CREATE DATABASE autocomplete; -- If not already created via UI
USE autocomplete;
CREATE TABLE search (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(50) NOT NULL
);
INSERT INTO search (Name) VALUES
('David Copperfield'),
('Ricky Ponting'),
('Cristiano Ronaldo'),
('Lionel Messi'),
('Shane Watson');
This creates a table search with an auto-incrementing ID and a Name field, then inserts five sample names into the table. Feel free to add more entries or different data relevant to your use case. The idea is to have a dataset to test our live search against. For example, we’ve added a mix of names (two are first+last names, and others are last names of famous individuals) so we can test partial matches.
Setup recap: At this point, you should have a running MySQL server with a database (e.g., autocomplete) that contains a table search populated with sample data. We will query this table based on user input from our live search box.
Our live search feature will consist of five components, each implemented as a separate file in the web server’s document root (for XAMPP, this is the htdocs directory):
Make sure to create these files in your server’s web directory (for example, C:\xampp\htdocs\ if using XAMPP on Windows). For clarity, you can put them in a folder (e.g., a folder named live-search/ in htdocs) and adjust paths accordingly when accessing in the browser. In this guide, we’ll assume they are directly in the root for simplicity.
Let’s go through each step and file one by one:

This file will produce the interface where users can type their queries and see suggestions. It includes an HTML search input, a results display area, and links to our script and style files. It also loads the jQuery library (which we’ll use for AJAX).
Create a file search.php with the following content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Live Search Example</title>
<!-- Include jQuery library (required for AJAX). Using a CDN for latest jQuery. -->
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<!-- Include our custom JavaScript file for handling the AJAX and UI logic. -->
<script src="script.js"></script>
<!-- Include our CSS for styling the search box and results. -->
<link rel="stylesheet" href="style.css" />
</head>
<body>
<!-- Search input box -->
<input type="text" id="search" placeholder="Search" autocomplete="off" />
<br />
<small><strong>Ex:</strong> <em>David, Ricky, Ronaldo, Messi, Watson</em></small>
<br />
<!-- Container to display suggestions -->
<div id="display"></div>
</body>
</html>
Explanation:
In the head section, we load jQuery (from a CDN) and then our custom script.js. We also link the stylesheet. In the body, there’s a text <input> with id=”search” where the user types their query. We gave it a placeholder “Search” and turned off autocomplete (to prevent the browser’s suggestions from interfering). Below the input, there’s a small example hint showing sample names the user could try (these correspond to the data we inserted in the database). Finally, we have a <div id=”display”></div>, which will act as a dropdown container for the search suggestions. Initially, this div is empty; it will be populated dynamically by our script when results come in.

Next, we need a way to connect to the MySQL database from our PHP scripts. The db.php file will establish the connection using PHP’s MySQLi extension.
Create a file db.php with the following code:
<?php
// Database connection settings
$host = "localhost"; // MySQL server host (usually localhost)
$user = "root"; // MySQL username (default XAMPP user is 'root')
$password = ""; // MySQL password (default XAMPP password is empty)
$dbname = "autocomplete"; // Database name
// Establish connection
$con = mysqli_connect($host, $user, $password, $dbname);
// Check connection
if (mysqli_connect_errno()) {
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
?>
Explanation:
Update the $host, $user, $password, and $dbname variables as needed for your setup. In XAMPP default configuration, a user is “root,” and the password is empty, and we named our database “autocomplete”. The code calls mysqli_connect to open a connection to MySQL and then checks for any connection error. If the connection fails, it uses die() to output an error message and stop execution. By including this file in other scripts, we can reuse the $con connection object for queries.
Security Note: In a real-world application, avoid hardcoding credentials in a publicly accessible file. Consider moving these to a secure configuration or environment variables. Also, ensure the db.php file is not directly accessible via URL (you might place it outside the web root or restrict access) since it contains sensitive info.

This PHP file will handle AJAX requests from the client-side script. When the user types something, our JavaScript will send the input to this script (behind the scenes). The script will then query the database for matching entries and output an HTML snippet (list of results), which the client script will display.
Create a file ajax.php with the following content:
<?php
// Include the database connection file
include "db.php";
// Only proceed if the "search" parameter is provided (via POST)
if (isset($_POST['search'])) {
$term = $_POST['search']; // The search term from the AJAX request
// Basic query to fetch names that contain the search term (case-insensitive match)
$query = "SELECT Name FROM search WHERE Name LIKE '%" . mysqli_real_escape_string($con, $term) . "%' LIMIT 5";
$result = mysqli_query($con, $query);
// Start an HTML unordered list for results
echo "<ul>";
if ($result) {
// Check if we found any matches
if (mysqli_num_rows($result) > 0) {
// Fetch each matching record and output as a list item
while ($row = mysqli_fetch_assoc($result)) {
$name = htmlspecialchars($row['Name']); // escape output for safety
// Each list item calls a JavaScript function 'fill()' when clicked
echo "<li onclick=\"fill('{$name}')\">" . $name . "</li>";
}
} else {
// No matches found
echo "<li><em>No results found</em></li>";
}
}
echo "</ul>";
}
?>
Explanation:
This script returns a snippet of HTML (the <ul> and <li> items) as the response to the AJAX call. Our front end will receive this and insert it into the page.
Improvement Note: In a production scenario, it’s better to use prepared statements for queries rather than directly concatenating the input into SQL, as that provides stronger protection against SQL injection attacks. Here, we’ve used mysqli_real_escape_string for basic safety. Using prepared statements (with placeholders for the search term) or parameterized queries (e.g., PDO or MySQLi prepared statements) would be the recommended approach for robust security. Also, consider performance: adding an index on the Name column or using a full-text search might be useful if your dataset is large.

Now, we write the client-side logic to tie everything together. This script will listen for input events on the search box, send the typed query to ajax.php via an AJAX request, and display the results returned by the server. It will also define the fill() function used when a suggestion is clicked.
Create a file script.js with the following code:
// This function is called when a user clicks on a suggestion from the list.
// It fills the clicked value into the search box and then hides the suggestion list.
function fill(value) {
$('#search').val(value);
$('#display').hide();
}
$(document).ready(function() {
// When the user releases a key in the search box:
$("#search").keyup(function() {
let query = $(this).val().trim(); // get the current text in the search input
if (query === "") {
// If the input is empty, clear any existing results
$("#display").empty().hide();
} else {
// If there is input, send an AJAX POST request to the server
$.ajax({
type: "POST",
url: "ajax.php",
data: { search: query },
success: function(response) {
// On success, display the returned HTML (list of suggestions)
$("#display").html(response).show();
}
});
}
});
});
Explanation:
Usage Note: The use of keyup means the search happens on every key release. This provides real-time results, but in a very busy database or with many users, you might want to implement a slight delay or debounce to prevent too many rapid requests. For example, you could wait 300ms after typing stops before sending the request. In our simple implementation, we don’t debounce for the sake of clarity. Also, we limit results to 5 in the SQL query to avoid overwhelming the interface with too many suggestions.

Finally, a bit of CSS will make the search box and result list look presentable. You can style this as you like; we’ll do a basic styling where the suggestions appear as a dropdown list below the input.
Create a file style.css with some basic styles:
/* Style the suggestions dropdown */
#display ul {
list-style: none;
margin: 0;
padding: 0;
border: 1px solid #ccc;
max-width: 250px; /* adjust to the width of your input if needed */
background: #fff;
position: absolute;
z-index: 1000;
}
#display li {
padding: 8px 12px;
border-bottom: 1px solid #ccc;
/* Optionally, you can match the font to the input's font */
}
#display li:last-child {
border-bottom: none;
}
#display li:hover {
cursor: pointer;
background-color: #f0f0f0;
}
Explanation:
Feel free to adjust the styles (colors, spacing, widths) to fit your design. The key is to ensure the suggestions are clearly visible and look connected to the search box.
With all files in place, it’s time to test the functionality.
1. Open the page in your browser: Navigate to the search.php file through your local server. For example, if you’re using XAMPP and placed the files in htdocs, go to http://localhost/search.php (or http://localhost/live-search/search.php if you put them in a folder). You should see the search input box (and the example hints we added below it).
2. Try out the live search: Start typing one of the sample names, such as “Ri” for “Ricky Ponting” or “Ron” for “Cristiano Ronaldo”. As you type, you should see a list of matching names appear below the input. The results update with each keystroke. For example, typing “Ron” will quickly show “Cristiano Ronaldo” as a suggestion. Typing “Lion” will show “Lionel Messi”. If you type something that doesn’t match any name (e.g., “XYZ”), the script will display “No results found”.
Each suggestion in the list is clickable. If you click on “Lionel Messi” in the dropdown, for instance, that name will be inserted into the search box (and the suggestion list will disappear). This mimics the autocomplete behavior. From here, you could extend the logic to, say, submit the form or navigate to a detailed page for that selection when clicked or when the user presses Enter. In our basic implementation, we stop at showing how to populate the suggestion.

How it works under the hood: Once you start typing, the following sequence happens for every key press:
Troubleshooting: If nothing happens, check the browser’s developer console for errors. Common issues might be:
Congratulations! You’ve built a functional live search box that updates results in real time as the user types. This feature makes your web application more interactive and user-friendly. By leveraging AJAX (as implemented with jQuery in our case), we avoided full-page reloads and provided a smoother search experience. On the server side, PHP handled the incoming requests and queried the MySQL database for relevant data. This trifecta of web technologies (PHP, MySQL, JavaScript) is a classic way to add dynamic behavior to websites and remains very applicable to modern development.
To recap, a live search works by sending the current query to the server on each keystroke and fetching matches. Using jQuery, we simplified the AJAX call and DOM updates. However, it’s worth noting that you can achieve the same result with modern vanilla JavaScript. The Fetch API, for instance, is a modern replacement for the older XMLHttpRequest-based approach. Instead of using jQuery, we could use fetch() to send the request and update the page.
Regardless of the method, the core principle is the same. You now have a foundation that you can extend and customize:
By building this yourself, you should have a clearer understanding of how live search/autocomplete features work behind the scenes. It’s a powerful technique to enhance user interaction on data-driven websites. Happy coding!

Hassan Tahir wrote this article, drawing on his experience to clarify WordPress concepts and enhance developer understanding. Through his work, he aims to help both beginners and professionals refine their skills and tackle WordPress projects with greater confidence.