Is it possible to create expandable links in PHP to display details from a database without reloading the entire page?
To create expandable links in PHP to display details from a database without reloading the entire page, you can use AJAX to fetch the details dynamically and update the content on the page. This allows for a more seamless user experience without the need to reload the entire page each time a link is clicked.
<?php
// Include your database connection file here
// Fetch data from database based on the link clicked
if(isset($_GET['id'])) {
$id = $_GET['id'];
// Query database for details based on $id
// Display details in the expandable section
echo "Details for ID $id";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Expandable Links</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('.expand-link').click(function(e) {
e.preventDefault();
var id = $(this).data('id');
$.ajax({
url: 'your_php_file.php',
type: 'GET',
data: {id: id},
success: function(response) {
$('#details').html(response);
}
});
});
});
</script>
</head>
<body>
<a href="#" class="expand-link" data-id="1">Expand Link 1</a>
<a href="#" class="expand-link" data-id="2">Expand Link 2</a>
<div id="details"></div>
</body>
</html>
Keywords
Related Questions
- What role does the .htaccess file play in website indexing and search engine optimization?
- Is it recommended to break down a long PHP script into smaller parts when running it through a Cronjob, or is it fine to let it run continuously?
- How can classes be misused in PHP, leading to a non-object-oriented approach?