How can PHP be used to pass parameters between pages in an image gallery?
To pass parameters between pages in an image gallery using PHP, you can use query strings in the URL. When a user clicks on an image, you can append the image ID as a parameter in the URL. On the target page, you can retrieve this parameter using $_GET and then use it to fetch the relevant image data from your database.
// Source page displaying the image gallery
<img src="image1.jpg" onclick="window.location.href='image_details.php?id=1'">
<img src="image2.jpg" onclick="window.location.href='image_details.php?id=2'">
<img src="image3.jpg" onclick="window.location.href='image_details.php?id=3'">
// image_details.php
<?php
if(isset($_GET['id'])){
$image_id = $_GET['id'];
// Use $image_id to fetch image details from database
}
?>
Keywords
Related Questions
- How can the code be optimized to prevent SQL injections and improve overall performance?
- How can conditional statements be used to handle user input in PHP scripts run in the console?
- Why is it recommended to use a Mailer class like SwiftMailer or PHPMailer instead of the mail() function in PHP for sending emails?