How can PHP be used to achieve real-time data updates without page reload?
To achieve real-time data updates without page reload in PHP, you can use AJAX (Asynchronous JavaScript and XML) to make requests to the server and update specific parts of the page dynamically. This allows you to fetch new data from the server without refreshing the entire page, providing a seamless user experience.
<?php
// PHP code to fetch data from the server
if(isset($_GET['getData'])) {
// Fetch data from the server
$data = fetchDataFromServer();
// Return the data as JSON
echo json_encode($data);
}
// Function to fetch data from the server
function fetchDataFromServer() {
// Code to fetch data from the server
$data = array(
'name' => 'John Doe',
'age' => 30
);
return $data;
}
?>