I'm trying to post a variable from one page to another in php using a button. I have retrieved the variable "Class" from the table "Class" and now want to POST it to the viewmembers.php page but I'm not sure how to do this.
This is the code:
<?php session_start(); include_once('connection.php'); $stmt = $conn->prepare("SELECT * FROM class WHERE Username = :Username"); $stmt->bindParam(':Username', $username); $stmt->execute(); while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { echo("Class Code: ".$row["Class"]." <br> Username: " .$row["Username"]." <br> Subject: ".$row["SubjectName"]. "<br>"); echo("<button onclick= \"location.href='viewmembers.php'\">View Members</button><br><br>"); } ?>
I have tried using session variables, but since I retrieve multiple rows from the table, the session variable only stores the last row retrieved from the table. Any help would be greatly appreciated.
It sounds like you just want the page to list each class and be able to have a link to the
viewmembers.php
page and send the class value to that page when clicked.Although you mentioned POST, it's generally more logical and simpler to implement using a GET request.
So instead of
You can write
Then the normal HTML hyperlink will be output in the following format
viewmembers.php
The page can read the class variables passed to it in the following waysin code. It can then use that value for any purpose you like (for example, by using it as a parameter in a SQL query to retrieve specific details about the class and its members, and display them).