What are the best practices for integrating AJAX requests in PHP and JavaScript, especially when dealing with search functionalities?

When integrating AJAX requests in PHP and JavaScript for search functionalities, it is important to ensure that the search query is properly sanitized to prevent SQL injection attacks. Additionally, the response from the server should be in a format that can be easily consumed by the client-side JavaScript code, such as JSON. Using PHP to handle the search query and return results to the client-side JavaScript via AJAX is a common practice.

<?php
// Assuming a database connection is established

$search_query = $_GET['search_query']; // Retrieve the search query from the AJAX request
$search_query = mysqli_real_escape_string($connection, $search_query); // Sanitize the search query

$query = "SELECT * FROM your_table WHERE column_name LIKE '%$search_query%'";
$result = mysqli_query($connection, $query);

$data = array();
while($row = mysqli_fetch_assoc($result)) {
    $data[] = $row;
}

echo json_encode($data); // Return the search results in JSON format
?>