How can one dynamically load content using a Pulldown with include in PHP?
To dynamically load content using a Pulldown with include in PHP, you can use JavaScript to detect the change in the Pulldown value and then make an AJAX request to load the corresponding content. The PHP script will include the appropriate file based on the value selected in the Pulldown.
<?php
if(isset($_GET['page'])) {
$page = $_GET['page'];
include($page . '.php');
}
?>
<select id="contentSelector">
<option value="page1">Page 1</option>
<option value="page2">Page 2</option>
</select>
<div id="content"></div>
<script>
document.getElementById('contentSelector').addEventListener('change', function() {
var page = this.value;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
document.getElementById('content').innerHTML = xhr.responseText;
}
};
xhr.open('GET', 'load_content.php?page=' + page, true);
xhr.send();
});
</script>
Keywords
Related Questions
- What are the potential pitfalls of using multiple mysql_result calls in a loop instead of mysql_fetch_array or mysql_fetch_object in PHP?
- What are the potential issues with PHP sessions being appended multiple times to URLs?
- How can PHP be used to address height discrepancies in elements within a login form?