Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Ajax Live Search in PHP

Ajax Live Search in PHP

Awesome choice! A Live Search with AJAX + PHP + MySQL lets users search and get results as they type, without reloading the page — just like Google search suggestions!


? What You'll Build

  • A text box where users type a keyword

  • PHP searches a MySQL database

  • AJAX fetches and displays matching results instantly


?? Step-by-Step Guide

? 1. Create the Database (MySQL)

CREATE DATABASE test_db;USE test_db;CREATE TABLE users (    id INT AUTO_INCREMENT PRIMARY KEY,    name VARCHAR(100));INSERT INTO users (name) VALUES('Alice'), ('Bob'), ('Charlie'), ('David'), ('Eve'), ('Frank');

? 2. HTML + jQuery (index.html)

<!DOCTYPE html><html><head>    <title>Live Search</title>    <style>        #result { border: 1px solid #ccc; max-width: 200px; padding: 5px; }    </style>    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script></head><body><h3>Search User:</h3><input type="text" id="search" autocomplete="off"><div id="result"></div><script>$(document).ready(function(){    $("#search").on("keyup", function(){        var query = $(this).val();        if (query !== "") {            $.ajax({                url: "search.php",                method: "POST",                data: {query: query},                success: function(data){                    $("#result").html(data);                }            });        } else {            $("#result").html("");        }    });});</script></body></html>

? 3. PHP Search Handler (search.php)

<?php$connect = new mysqli("localhost", "root", "", "test_db");if (isset($_POST['query'])) {    $search = $connect->real_escape_string($_POST['query']);    $result = $connect->query("SELECT name FROM users WHERE name LIKE '%$search%'");    if ($result->num_rows > 0) {        while ($row = $result->fetch_assoc()) {            echo "<p>" . $row['name'] . "</p>";        }    } else {        echo "<p>No results found</p>";    }}?>

? What’s Happening?

  1. User types in the input box.

  2. jQuery sends the keyword to search.php.

  3. PHP queries the database using LIKE '%keyword%'.

  4. Matching names are sent back and displayed instantly.


? Features You Can Add Next:

  • Highlight matching letters

  • Click-to-select a result

  • Debounce AJAX calls (for performance)

  • Use JSON response for more structured data

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql