I pass an individual filename as a session to my HTML form name. My first code prints a list of the files in my directory "diary", and I am trying to print the contents of the chosen file in a new page readpage.html But to my horror, since I am passing the session $_SESSION['filename'] as a variable, all my buttons direct me to the last file that my session variable has been assigned to.
What can I do so that each button passes the a different filename? I have found it impossible to pass a php variable as my form input name, since $variable = $_POST[PHP VARIABLE] doesn't work..
note) I'm trying to achieve this by using PHP only (no javascript, mysql tables etc) Thank you
$folder = "diary";
$files = scandir($folder);
$fName = "diary/$file";
?><form method="POST" action="readPage.html"><?
foreach ($files as $file)
{
if($file != "." && $file != "..")
{
$_SESSION["file"] = $file;
$data = file_get_contents("diary/$file");
$info = explode("\t",$data);
?>
<input type="radio" name = <?=$_SESSION["file"] ?> >
<?=$info[0]?> <input type="submit" value = "Go" ><br>
<?
}
}
?></form><?
Receving file readpage.html
echo $_SESSION["file"];
$fOpen = "diary/".$_SESSION["file"];
$fData = file_get_contents($fOpen);
You opened up a php segment here with
< ?, when it should be < ?php
this
? > < form method="POST" action="readPage.html" > < ?php
not this
? > < form method="POST" action="readPage.html" > < ?
You do not need to use session. Instead of $_SESSION["file"] use $_POST["file"]. I added name attribute name="file" to your input and as value attribute I added file name. In processing page you can accesss file name in variable $_POST["file"].
$folder = "diary";
$files = scandir($folder);
$fName = "diary/$file";
?><form method="POST" action="readPage.html"><?
foreach ($files as $file)
{
if($file != "." && $file != "..")
{
$data = file_get_contents("diary/$file");
$info = explode("\t",$data);
?>
<input type="radio" name="file" value="<? echo $file ?>" >
<?=$info[0]?> <input type="submit" value = "Go" ><br>
<?
}
}
?></form><?
To process form:
echo $_POST["file"];
$fOpen = "diary/".$_POST["file"];
$fData = file_get_contents($fOpen);
Of course you should change form action to php script readPage.html to readPage.php