AdyBlaze AdyBlaze
Latest Article

PHP Live Search using AJAX (2026) – Complete Beginner Guide

01 Aug 2026

WhatsApp Facebook Telegram


PHP Live Search using AJAX (2026)

Modern websites display search results instantly while users type. This feature is known as Live Search or AJAX Search.

Instead of refreshing the entire page, AJAX sends requests to the server in the background and updates the results dynamically. This creates a faster and smoother user experience.

In this tutorial, you’ll learn how to build a PHP Live Search using AJAX and MySQL using PHP 8.x.


What is AJAX?

AJAX (Asynchronous JavaScript and XML) allows JavaScript to communicate with the server without reloading the web page.

With AJAX you can:

Read this also : PHP Login System with MySQL – Complete Beginner Guide (2026)


How Live Search Works

  1. User starts typing in the search box.
  2. JavaScript sends the keyword to PHP using AJAX.
  3. PHP searches the MySQL database.
  4. Matching records are returned.
  5. Results appear instantly without refreshing the page.

Project Structure

live-search/
│
├── db.php
├── index.php
├── search.php
├── script.js
└── style.css

Step 1: Create Database

CREATE DATABASE ajax_search_db;

Create table:

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(100)
);

Insert sample records:

INSERT INTO users(name,email)
VALUES
('John Doe','john@example.com'),
('Adil Rasheed','adil@example.com'),
('Sara Khan','sara@example.com'),
('Ali Ahmad','ali@example.com');

Step 2: Database Connection (db.php)

<?php

$conn = mysqli_connect(
"localhost",
"root",
"",
"ajax_search_db"
);

if(!$conn){
die("Connection Failed");
}

?>

Step 3: Create Search Box (index.php)

<!DOCTYPE html>
<html>

<head>

<title>PHP AJAX Live Search</title>

<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>

<script src="script.js"></script>

</head>

<body>

<h2>Live Search</h2>

<input
type="text"
id="search"
placeholder="Search User...">

<div id="result"></div>

</body>

</html>

Step 4: AJAX Code (script.js)

$(document).ready(function(){

$("#search").keyup(function(){

var keyword = $(this).val();

$.ajax({

url:"search.php",

method:"POST",

data:{keyword:keyword},

success:function(data){

$("#result").html(data);

}

});

});

});

Whenever the user types, AJAX sends the keyword to PHP automatically.


Step 5: Search Data (search.php)

<?php

include "db.php";

$keyword = $_POST['keyword'];

$sql = "SELECT *
FROM users
WHERE name LIKE '%$keyword%'
OR email LIKE '%$keyword%'";

$result = mysqli_query($conn,$sql);

while($row=mysqli_fetch_assoc($result))
{

echo "<p>";

echo $row['name'];

echo " - ";

echo $row['email'];

echo "</p>";

}

?>

Now search results appear instantly.


Add CSS

#search{

width:300px;

padding:10px;

font-size:16px;

}

#result{

margin-top:20px;

border:1px solid #ddd;

padding:10px;

}

Prevent SQL Injection

Instead of directly inserting user input into the query, use prepared statements.

$stmt = mysqli_prepare(
$conn,
"SELECT *
FROM users
WHERE name LIKE ?
OR email LIKE ?"
);

$search = "%".$keyword."%";

mysqli_stmt_bind_param(
$stmt,
"ss",
$search,
$search
);

mysqli_stmt_execute($stmt);

$result =
mysqli_stmt_get_result($stmt);

This is the recommended approach for production websites.


Show “No Results Found”

if(mysqli_num_rows($result)>0){

while($row=mysqli_fetch_assoc($result))
{

echo $row['name']."<br>";

}

}else{

echo "No Results Found.";

}

Advantages of Live Search


Common Mistakes

Missing jQuery

If jQuery isn’t loaded, AJAX won’t work.


Wrong File Path

Ensure search.php and script.js are in the correct location.


Empty Search Results

Check:


SQL Injection Risk

Never use user input directly in SQL queries. Use prepared statements.


Best Practices


FAQs

What is Live Search?

Live Search displays search results while the user is typing without reloading the page.


Why Use AJAX?

AJAX allows communication with the server in the background, making applications faster and more interactive.


Can I Search Multiple Columns?

Yes. You can search by name, email, phone number, username, or any other database field.


Does Live Search Work on Mobile?

Yes. AJAX Live Search works on desktop, tablet, and mobile devices.


Conclusion

PHP Live Search using AJAX is an essential feature for modern web applications. By combining PHP, MySQL, AJAX, and jQuery, you can build a fast, responsive search experience that updates results instantly without refreshing the page. For production applications, always use prepared statements, validate user input, and optimize database queries for the best performance.