简体   繁体   中英

Is it possible to get the image src from previous page and display it in new page?

Is there a way to display an image that was clicked on a previous page to a new page? For example if I have step-2.php make a user pick an image and when clicked it goes to step-3.php and display it there. I hope I explained myself. Please Any help is appreciated.

This is the code from step-2

<a href="../business_cards/step-3.php">
<img src="../img/5.jpg" alt="Image1">
</a>

I want that click to sent the img src to step-3 if possible. This is what I have in step-3

<img src="../img/5.jpg" alt="imageCanvas">

EDIT Trying to do the $_SESSION method

<?php
  session_start();
  $_SESSION["imageid"] = "image url";
?>
<a href="step-3.php?imageid=../img/5.jpg">
<img src="../img/5.jpg" alt="Image1">
</a>

step2 link to step 3 with the imgsrc being in the query string.

// encode the query string first
<a href="../business_cards/step-3.php?imgsrc=<?=urlencode('../img/5.jpg')?>">

step3 relays the get data into an html element

// query string is automatically decoded with GET or POST methods 
<img src="<?=$_GET['imgsrc']?>" alt="imageCanvas">

Store your image URL in a session like this for clean URLs:

session_start(); // At the top of your header file
$_SESSION["imageid"] = "image url";

// Next page:
$imageurl = $_SESSION["imageid"]; // This in the next page
echo "<img src='$imageurl' />";

Or pass it on with a query string like this:

Header("Location: https://www.website.com/page.php?imageid=imageurl");
// Or
<a href="https://www.website.com/page.php?imageid=imageurl">Next page</a>

// Next page:
$imageurl = $_GET["imageid"]; // This in the next page
echo "<img src='$imageurl' />";

If you want a clean URL and you want to use it on multiple places use the $_SESSION method, else just use the $_GET method, choose wisely...

The easiest way would be to add a _get variable to your link, in the href add something like

href="code.php?image=<?php echo $image_src?>"

on the following page the image can be found in

$_GET['image']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM