How can AJAX be used to prevent page reload when filtering data in PHP?

When filtering data in PHP, AJAX can be used to prevent page reload by sending asynchronous requests to the server and updating the content dynamically. This allows for a smoother user experience without the need for the entire page to reload each time a filter is applied.

<?php
// PHP code to handle AJAX request for filtering data

if(isset($_POST['filter'])) {
    // Perform filtering logic here based on the filter criteria
    $filteredData = array(); // Example filtered data

    // Return the filtered data as JSON response
    header('Content-Type: application/json');
    echo json_encode($filteredData);
    exit;
}
?>