R
Rishtaara
PHP Fundamentals
Lesson 32 of 32Article22 min

AJAX Live Search, Poll & Course Wrap-up

Live search sends a request on every keystroke (with debounce). PHP filters database rows and returns JSON matches.

AJAX Live Search

Live search sends a request on every keystroke (with debounce). PHP filters database rows and returns JSON matches.

Real-life example: Live search is Google suggestions — type one letter and the list updates instantly.

Live search HTML + JS
<input type="text" id="search" placeholder="Search courses..." />
<ul id="results"></ul>
<script>
  let timer;
  document.getElementById("search").addEventListener("input", (e) => {
    clearTimeout(timer);
    timer = setTimeout(async () => {
      const q = e.target.value;
      if (q.length < 2) return;
      const res = await fetch(`/live-search.php?q=${encodeURIComponent(q)}`);
      const data = await res.json();
      document.getElementById("results").innerHTML =
        data.map((r) => `<li>${r.title}</li>`).join("");
    }, 300);
  });
</script>
live-search.php
<?php
header("Content-Type: application/json");
require "db.php";
$q = "%" . trim($_GET["q"] ?? "") . "%";
$stmt = $pdo->prepare("SELECT title FROM courses WHERE title LIKE :q LIMIT 8");
$stmt->execute([":q" => $q]);
echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));

AJAX Poll

A poll lets users vote without page reload. JavaScript POSTs the choice; PHP saves to MySQL and returns updated counts as JSON.

Real-life example: An AJAX poll is raising hands in a webinar chat — votes tally live without stopping the speaker.

vote.php
<?php
header("Content-Type: application/json");
require "db.php";

if ($_SERVER["REQUEST_METHOD"] === "POST") {
  $option = (int) ($_POST["option"] ?? 0);
  $stmt = $pdo->prepare("UPDATE poll SET votes = votes + 1 WHERE id = :id");
  $stmt->execute([":id" => $option]);
}

$counts = $pdo->query("SELECT id, label, votes FROM poll")->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($counts);
Vote with fetch
async function vote(optionId) {
  const body = new FormData();
  body.append("option", optionId);
  const res = await fetch("/vote.php", { method: "POST", body });
  const results = await res.json();
  renderChart(results);
}

Examples & Reference overview

You now cover the full Rishtaara PHP syllabus — basics through AJAX. Build small projects: contact form, login, CRUD app, live search.

Real-life example: Finishing this course is earning a driver's license — next you practice on real roads (Laravel, WordPress, APIs).

  • PHP manual: php.net — search any function
  • MySQL docs for advanced queries and indexes
  • Practice CRUD on a student or product table
  • Combine sessions + PDO + AJAX for a mini dashboard

Certificate, practice & what's next

Test your knowledge with MCQs and interview questions. Then explore Laravel for larger apps or WordPress for CMS work.

Real-life example: MCQs are a mock exam before the real job interview — find gaps while it is still safe.

  • MCQ practice: /mcq/php-mcq
  • Interview prep: /interview/php-interview
  • Knowledge guide: /knowledge/php-fundamentals
  • Next course: Laravel or full-stack projects on Rishtaara
Congratulations — you completed all 32 PHP lessons. Keep building projects; that is where syntax becomes skill.