0

I'm not sure how to pass the value of $tid to another php file. Well, I do know how but the thing is I want to have the button store the current $tid since it's gonna change after the next loop. If I use sessions and just call it from another file, $tid's value will be that of the last iteration.

    $select = mysqli_query($conn, "SELECT  Sport, TeamID, TeamAcadYear FROM team");

    echo "<html> <body> <table align='center'>";

    while($row = mysqli_fetch_array($select)){
        $sportName = $row['Sport']; 
        $tay = $row['TeamAcadYear'];
        $tid = $row['TeamID'];

        echo " <tr> <td> <button type='button'> $sportName" ." " ."$tay </button> </td> </tr>";
    } 
    echo "</table> </body> </html>";
Louise
  • 11
  • 5
  • What's the var used for on the next page? – Joe Keene May 16 '17 at 14:55
  • @JoeKeene Im going to use it for a mysql statement. ill use the TeamID (tid) thats passed to select a team that has that ID and then display the info of that team – Louise May 16 '17 at 15:01
  • Are you sending the user to the new page? – Joe Keene May 16 '17 at 15:02
  • @JoeKeene Yes, redirect to another pahe – Louise May 16 '17 at 15:03
  • 2
    http://stackoverflow.com/questions/871858/php-pass-variable-to-next-page – mxr7350 May 16 '17 at 15:05
  • @mxr7350 I know how to use sessions, cookies, etc. The problem is that as the while loop iterates, the tid changes. What happens if there are three rows retrieved from the select statement but the user clicks the first button. How can I get the tid of that row and pass it to another file? – Louise May 16 '17 at 15:13
  • @mxr7350 And how do you access an HTML tags `id` value in PHP? Do please enlighten me – RiggsFolly May 16 '17 at 15:23

2 Answers2

0

You can use $_GET.

For example:

Page 1:

<a href='page2.php?id=$id'><button type='button'> $sportName" ." " ."$tay </button></a>

Page 2:

To get the ID:

$idFromPage1 = $_GET['id'];

echo $idFromPage1; // Your ID from page 1 will be displayed
Edward
  • 2,291
  • 2
  • 19
  • 33
  • I can't think of a single situation where `$_GET` is the best way of passing data between pages. – Martin May 16 '17 at 15:54
  • @Martin Post/Redirect/Get https://en.wikipedia.org/wiki/Post/Redirect/Get - Maybe a search page? – Edward May 16 '17 at 16:04
-1

You're correct $_SESSION would be the ideal way, another way is to use GET or POST to send the value to another page. Could you name the $_SESSION vars like currentTid and previousTid etc?

Joe Keene
  • 2,175
  • 21
  • 27