Joe
Joe

Reputation: 23

Exchanging variables between two pages PHP

I am aware that this kind of question has already been asked before, but I have a slightly different case.

foreach($dir as $file) 
{ 
    $file = '<li><a href="test.php?file='.basename($file).'">'.basename($file).'</a></li>';
    echo $file; 
} 

This is my script to display files in a folder and link to them. The way it is now, I use the $_GET['file'] on the other page to receive the information on the other page. The other page is supposed to display a photo/video with the file that has been linked, however I don't know how to use the $_POST or $_SESSION in this case, since it's a loop and I don't want the information about the file be in the link.

Also, I don't want any forms. I want to click the link with the name of the file and the other website to already have the information about the file and display the video or image.

Upvotes: 2

Views: 61

Answers (1)

Sanjay Kumar N S
Sanjay Kumar N S

Reputation: 4749

Use like this by storing the media path in session and use the corresponding index to pass.

First file:

<?php
$i = 1;
session_start();
$dir = array('1.jpg', '2.jpg', '3.jpg');
echo '<ul>';
foreach($dir as $file) 
{ //echo $i.'---'.$file."<br />";
    echo '<li><a href="test.php?file='. $i .'">'.$file.'</a></li>';
    $_SESSION['media_'.$i] = $file;
    $i++;
} 
echo '</ul>';
?>

test.php

<?php
session_start();
echo $_SESSION['media_'.$_GET['file']];
?>

Upvotes: 1

Related Questions