How can JavaScript be utilized to sort data without reloading the page in a PHP application?

To sort data without reloading the page in a PHP application, JavaScript can be used to dynamically sort and display the data based on user input. This can be achieved by sending an AJAX request to the server to fetch the sorted data and then updating the content on the page without refreshing.

<?php
// PHP code to handle sorting request and return sorted data

// Check if a sorting parameter is passed through AJAX
if(isset($_GET['sort_by'])) {
    $sort_by = $_GET['sort_by'];
    
    // Perform sorting logic based on $sort_by parameter
    
    // Return sorted data as JSON response
    echo json_encode($sorted_data);
}
?>