PHP AJAX Live Search Example

PHP AJAX Live Search Example
AJAX Live Search allows users to search data in real time as they type—without reloading the page.
This is very common in search boxes, user lookup, product search, admin panels, etc.
What is AJAX Live Search?
User types in input box
AJAX sends request on each key press
PHP fetches matching data
Results appear instantly below input
Also called Autocomplete / Instant Search
How It Works (Flow)
User types text
JavaScript sends AJAX request
PHP queries database
Matching records returned
HTML updates dynamically
Database Table (Example)
students table
Sample data:
HTML + JavaScript (AJAX)
index.html
PHP File (Search Logic)
search.php
%→ wildcard$q%→ starts with typed text
Output Example
Typing “Ra” shows:
- No page reload
- Real-time results
Improve Search (Anywhere Match)
Matches text anywhere in the name
Security Improvement (Interview)
Use Prepared Statements to prevent SQL Injection.
Common Mistakes
- Query on every keystroke without limit
- No input validation
- SQL injection risk
- Not handling empty input
- No index on searched column
AJAX Live Search vs Normal Search
| Feature | Live Search | Normal Search |
|---|---|---|
| Page reload | No | Yes |
| User experience | Fast | Slower |
| Server calls | More | Less |
| Modern apps | Yes | Old |
Interview Questions & MCQs
Q1. What is AJAX Live Search?
A) Page reload search
B) Real-time search
C) Static search
D) Server-side search only
Answer: B
Q2. Which event is commonly used?
A) onclick
B) onchange
C) onkeyup
D) onsubmit
Answer: C
Q3. Which operator is used in SQL search?
A) IN
B) BETWEEN
C) LIKE
D) EXISTS
Answer: C
Q4. Best way to prevent SQL injection?
A) mysqli_query
B) Prepared statements
C) echo
D) htmlspecialchars
Answer: B
Q5. Live search improves?
A) Database size
B) UI/UX
C) Server load
D) Storage
Answer: B
Real-Life Use Cases
- Google-like search
- Admin user lookup
- Product search
- Email / username suggestions
Summary
AJAX Live Search = real-time search
Uses JavaScript + PHP + MySQL
LIKEoperator for matchingNo page reload
Prepared statements for security
Very important for interviews & projects
