I have an array and i need to display all values, so i used a foreach loop. I need that when the user click on the button " READ " the value passed to oldvisit.php
is the value of that row, i tried with SESSION but the value passed is the last of the Array. Of course the values displayed in the page is the right value, but how can i take it and send to oldvisit.php
? I cant use the form method, because these lines of code, are still inside a Form. I cant find a solution from days!
<?php
foreach($Visits as $x => $x_value) {
$_SESSION['data'] = $x;
echo "<li><center>". $x . " <input type='button' value='READ' onclick='window.location.href=\"oldvisit.php\" '></li>";
}
?>
Your current code overwrites $_SESSION['data']
on each loop and therefore $_SESSION['data']
will always be set to the last loop value. I'm assuming the keys on $Visits
($x
on your code) are the identifiers you want to use.
index.php
<?php
foreach($Visits as $key => $value)
{
echo "<li>" . $key . " <input type='button' value='READ' onclick='window.location.href=\"oldvisit.php?id=".$key."\"'></li>";
}
?>
oldvisit.php
<?php
echo $_GET['id']; // Remember security (sql injection, etc...)
?>
This example uses the query string (everything after ? in the url) to pass data between scripts. Paramerters in the query string can be accessed using the reserved variable $_GET[]
.