I am trying to use a button to POST a variable from one page in php to another. I have retrieved the variable "Class" from the table "Class" and now want to POST it to the viewmembers.php page, however am unsure how to do this.
Here 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, however since I have retrieved multiple rows from the table, the session variable only stores the last row that was retrieved from the table. Any help would be appreciated.
2
Answers
It sounds like you just want your 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 it’s clicked on.While you mention a POST, this is generally more logical to achieve with a GET request – and simpler as well.
So instead of
you could write
which would then output a normal HTML hyperlink in the format
The
viewmembers.php
page can then read the class variable passed to it by havingin the code. It can then use that value for whatever purpose you like (e.g. by using it as a parameter in a SQL query to retrieve specific details about that class and its members, and display them).
I’m not sure to understand.
The href link uses the GET method.
For a POST method you can use some form(s) like below with the parameters you need.