What role does AJAX play in creating auto suggest/auto complete features in PHP?

AJAX plays a crucial role in creating auto suggest/auto complete features in PHP by allowing the web page to communicate with the server in the background without the need to refresh the entire page. This enables real-time suggestions to be fetched from the server as the user types in the input field, providing a seamless and interactive user experience.

<?php
// PHP code for auto suggest/auto complete using AJAX

// Check if the search term is set
if(isset($_GET['term'])){
    // Connect to database
    $conn = new mysqli('localhost', 'username', 'password', 'database');

    // Retrieve search term
    $searchTerm = $_GET['term'];

    // Query to fetch suggestions based on search term
    $query = "SELECT * FROM products WHERE name LIKE '%".$searchTerm."%'";

    // Execute query
    $result = $conn->query($query);

    // Store suggestions in an array
    $suggestions = array();
    while($row = $result->fetch_assoc()){
        $suggestions[] = $row['name'];
    }

    // Output suggestions as JSON
    echo json_encode($suggestions);
}
?>