How can you refresh a table in PHP without reloading the entire page?

To refresh a table in PHP without reloading the entire page, you can use AJAX to fetch updated data from the server and dynamically update the table content. This allows for a smoother user experience without the need for a full page reload.

<!DOCTYPE html>
<html>
<head>
    <title>Refresh Table Data with AJAX</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>

<table id="data_table">
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Email</th>
    </tr>
    <?php
    // Display initial table data here
    ?>
</table>

<script>
$(document).ready(function(){
    function refreshTable(){
        $.ajax({
            url: 'fetch_data.php', // PHP script to fetch updated data
            success: function(data){
                $('#data_table').html(data); // Update table content with fetched data
            }
        });
    }
    
    setInterval(refreshTable, 5000); // Refresh table every 5 seconds
});
</script>

</body>
</html>