PHP AJAX Live Search
In this page:
$query = $_GET["q"] ?? "";
$matches = array_filter($items, fn($item) => stripos($item, $query) !== false);
header("Content-Type: application/json");
echo json_encode(array_values($matches));
The Basic Live Search Structure
A live search interface has three pieces working together: an <input> field the user types into, a JavaScript listener that fires an AJAX request as the text changes, and a PHP endpoint that searches the database and returns matches to display.
Example: The Basic Live Search Structure
<?php
// Set `$_GET['q']` to "ph"
$_GET['q'] = "ph";
// Declare `$products` as an array: `["php", "python", "java"]`
$products = ["php", "python", "java"];
// Declare `$matches`, set to `array_filter($products, function ($p) { return strpos($p, $_GET['q']) !== false; })`
$matches = array_filter($products, function ($p) { return strpos($p, $_GET['q']) !== false; });
// Print `json_encode(array_values($matches))` to the output
echo json_encode(array_values($matches));
?>
Login to try C/C++/Java/PHP code in the editor
Listening for Input and Triggering a Search
JavaScript's input event fires every time the text field's value changes, including single keystrokes and pastes -- the natural event to listen for in a live search box, since it captures every way the search text can change.
Example: Listening for Input and Triggering a Search
<?php
// JS: input.addEventListener('input', () => fetch('search.php?q=' + input.value))
$_GET['q'] = "p";
echo json_encode(["query_received" => $_GET['q']]);
?>
Login to try C/C++/Java/PHP code in the editor
Debouncing Search Requests
Debouncing delays sending the search request until the user has paused typing for a short moment (like 300 milliseconds) -- if a new keystroke arrives before that delay finishes, the pending request is cancelled and the timer restarts, dramatically reducing the number of requests sent while typing quickly.
Example: Debouncing Search Requests
<?php
// JS: clearTimeout(timer); timer = setTimeout(() => search(value), 300);
echo "Debouncing happens in JavaScript before the PHP endpoint is ever called";
?>
Login to try C/C++/Java/PHP code in the editor
Displaying Live Search Results
Once the AJAX response arrives with matching results, JavaScript updates the page's results container -- typically clearing out any previous results first, then building new list items for each match returned by the PHP endpoint.
Example: Displaying Live Search Results
<?php
$products = ["php", "phpunit", "phpmailer"];
echo json_encode($products);
// JS clears the results container, then builds a list item per match
?>
Login to try C/C++/Java/PHP code in the editor
Handling Out-of-Order Responses
Because requests fire asynchronously, a slower response to an earlier (shorter) search term can arrive after a faster response to a later, more specific one -- overwriting the correct, current results with stale ones.
Tracking a request identifier and ignoring any response that is not the most recent request avoids this.
Example: Handling Out-of-Order Responses
<?php
$_GET['requestId'] = "3";
$_GET['q'] = "php";
echo json_encode(["requestId" => $_GET['requestId'], "results" => ["php", "phpunit"]]);
// JS ignores any response whose requestId isn't the most recent one sent
?>
Login to try C/C++/Java/PHP code in the editor
- Firing an AJAX request on every single keystroke without any debouncing, flooding the server with requests for a user typing quickly.
- Building the live search database query without a prepared statement, since the search term comes directly from unpredictable, fast-changing user input.
- Not handling out-of-order responses, where a slower request for an earlier, shorter search term arrives after a faster request for a later, more specific one, showing stale results.
- A live search box listens for keyup/input events and sends an AJAX request with the current search text on each change.
- Debouncing delays firing the search request until the user pauses typing briefly, avoiding a request on every single keystroke.
- The PHP endpoint safely queries the database with the search term using a prepared statement and LIKE, returning matches as JSON.
Live search relies only on standard AJAX (fetch) and PHP database querying, both fully supported in every modern browser and PHP environment.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: