Dynamic Content Loading
☰Fullscreen
Table of Content:
Example 1: Dynamic Content Loading
Objective: Load dynamic content without refreshing the entire page.
HTML (index.html):
Dynamic Content Example
PHP (content.php):
JavaScript (script.js):
function loadDynamicContent() { // Using jQuery for AJAX request $.ajax({ url: 'content.php', type: 'GET', success: function(response) { // Update the content container with the retrieved data $('#dynamic-content-container').html(response); }, error: function(error) { console.log('Error:', error); } }); }
Explanation:
- Clicking the "Load Content" button triggers the
loadDynamicContentJavaScript function. - The function makes an AJAX request to the
content.phpfile. - The PHP file generates dynamic content (in this case, a timestamp).
- The JavaScript function updates the content container with the received data without refreshing the entire page.
This example demonstrates how PHP can dynamically generate content, and AJAX helps in updating specific parts of the page asynchronously.