-4

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.

Apodemus
  • 579
  • 2
  • 19
  • 2
    `location.href=...` will only cause a _GET_ request for the assigned URL. _"however am unsure how to do this."_ - create a `form`, method=post, action=url of the script you want to send it to, and stick the parameter value you need to transport into a hidden field, or make it the `value` of your button ... – CBroe Sep 01 '23 at 11:54
  • @Cbroe - I don't want to use a form – Cruisey123 Sep 01 '23 at 11:55
  • What have you tried to resolve the problem? Where are you stuck? – Nico Haase Sep 01 '23 at 11:57
  • 2
    But that _is_ really about your only option, if you directly want to post data from one page to the next. Sure, you could make an AJAX POST request in the background - but then you would still be on the first page when that request finishes, and would still have to go to the 2nd page - and then in there, pull the value out of the session, that you stored in there with your AJAX request ... – CBroe Sep 01 '23 at 11:57
  • I have tried to use session variables however since I may retrieve multiple rows from the table, the session variable will only hold the last row to be retrieved. – Cruisey123 Sep 01 '23 at 11:58
  • If you don't mind using a GET, you can simply make PHP output a hyperlink containing the value as a URL parameter, like `View Members`. I'd say a GET probably makes more sense in this situation anyway, rather than POST. So `echo 'View Members';` – ADyson Sep 01 '23 at 11:59
  • 1
    _Why_ do you want this to be a POST request in the first place? This is, apparently, a script to _view_ data(?) - so it should be requested using GET in the first place -> [When do you use POST and when do you use GET?](https://stackoverflow.com/q/46585/1427878) – CBroe Sep 01 '23 at 11:59
  • 1
    _"however since I may retrieve multiple rows from the table, the session variable will only hold the last row to be retrieved"_ - if you want to somehow "select" multiple rows, _before_ the data is submitted, then you can't do that with a (submit) button on each row in the first place ... You'd need something like checkboxes to first _select_ the rows, and then submit that data set afterwards. (Which would be even more of a point for use an actual proper form ... which you rejected without even giving _any_ valid reason.) – CBroe Sep 01 '23 at 12:02
  • X/Y problem. What do you ACTUALLY want to achieve – mplungjan Sep 01 '23 at 13:01
  • 1
    @mplungjan I think they just want a list of classes, with a link for each one which would open a details page about that class. Bog-standard stuff, easily solved with a hyperlink. – ADyson Sep 01 '23 at 13:06

1 Answers1

1

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

echo("<button onclick= \"location.href='viewmembers.php'\">View Members</button><br><br>");

you could write

echo '<a href="viewmembers.php?class='.$row["Class"].'">View Members</a>';

which would then output a normal HTML hyperlink in the format

<a href="viewmembers.php?class=abc">View Members</a>

The viewmembers.php page can then read the class variable passed to it by having

$class = $_GET["class"];

in 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).

ADyson
  • 57,178
  • 14
  • 51
  • 63