HTML - Change Page Contents Dynamically Without Refreshing
Challenge:
You have data retrieved from a database and displayed in a div. When a link is clicked, you want to update the div's contents without refreshing the page.
Solution:
Leveraging AJAX, you can bypass page refreshes and update the div's content seamlessly. Here's how it works:
Client-Side (HTML and JavaScript):
Server-Side (PHP):
Example:
<a href="#" onClick="recp('1')" > One </a> <a href="#" onClick="recp('2')" > Two </a> <a href="#" onClick="recp('3')" > Three </a> <div>
function recp(id) { $('#myStyle').load('data.php?id=' + id); }
// In data.php $id = $_GET['id']; $results = mysql_query("SELECT para FROM content WHERE para_ID='$id'"); if( mysql_num_rows($results) > 0 ) { $row = mysql_fetch_array( $results ); echo $row['para']; }
By following these steps, you can update the div's contents dynamically without causing the entire page to refresh, making your website more responsive and user-friendly.
The above is the detailed content of How Can I Dynamically Update a Div's Content with AJAX Without Page Refresh?. For more information, please follow other related articles on the PHP Chinese website!