Reputation: 37
I have the following php code:
session_start();
include "config.php";
$sql="SELECT Photoid FROM photos";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc())
{
$name=$row["Photoid"];
echo '<img src="'.$name.'" / width="200px" height="200px" onclick="myFunction()">';
}
}else{
echo "0 results";
}
$conn->close();
?>
<script type="text/javascript">
function myFunction() {
location.href = 'photo-page.php';
}
</script>
With which i display some photos that i get from a database and then i make them clickable so i can be transferred to another page "photo-page.php". What i want to do is to know which of those photos that i display has been clicked in order to know which one i should display on the photo-page.
Upvotes: 0
Views: 92
Reputation: 7285
Use HTML anchors and append GET data:
<?php
// index.php
// ...
echo '<a href="photo-page.php?img-name='.$name.'">';
echo '<img src="'.$name.'" width="200px" height="200px">';
echo '</a>';
// ...
?>
In photo-page.php
, you can retrieve the name of the image via the $_GET
variable:
<?php
// photo-page.php
// ...
if (isset($_GET['img-name'])) {
$imageName = $_GET['img-name'];
// display the image in its full size
echo '<img src="'.$imageName.'">';
}
// ...
?>
Upvotes: 0
Reputation: 2408
Change
echo '<img src="'.$name.'" / width="200px" height="200px" onclick="myFunction()">';
To
echo '<img src="'.$name.'" / width="200px" height="200px" onclick="myFunction('.$name.')">'
And
function myFunction(name)
{
alert(name);
// location.href = 'photo-page.php';
}
Upvotes: 2