How can PHP be used to open a specific page (info.php) when clicking on an image and pass the person's ID from the image to the page?
To open a specific page (info.php) when clicking on an image and pass the person's ID from the image to the page, you can use JavaScript to handle the click event on the image and send an AJAX request to the info.php page with the person's ID as a parameter. In the info.php page, you can retrieve the person's ID from the request parameters and process it accordingly.
<!-- HTML code with image and JavaScript click event -->
<img src="person.jpg" id="personImage" data-person-id="123">
<script>
document.getElementById('personImage').addEventListener('click', function() {
var personId = this.getAttribute('data-person-id');
var xhr = new XMLHttpRequest();
xhr.open('GET', 'info.php?id=' + personId, true);
xhr.send();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
// Handle the response from info.php if needed
}
};
});
</script>
// info.php code to retrieve the person's ID
<?php
$personId = $_GET['id'];
// Process the person's ID as needed
?>
Related Questions
- How can multiple selections from a list be securely saved in PHP?
- How can a PHP script be designed to lock a file for other users when it is opened and unlock it after it has been saved, as discussed in the thread?
- What potential pitfalls should one be aware of when using header for redirection in PHP?