I have a(n) SQL table with the values Applicationid, username, first_Name, last_Name, and email. I want the records to be displayed on to a table along with 2 buttons (Accept and Reject, which is in 1 table data cell). I am able to display the records and the buttons, however whenever I press any of the buttons, I always get the values of whichever record was entered last.
What I was trying to do in my code was to put the id’s into an array, use session, and pass them over to their respective php files. However, I just realized that there’s no way for the code to know which button was pressed.
<tbody>
<tr>
<?php
while($row = mysqli_fetch_assoc($applicationResult)){ ?>
<td><?php echo $row['FirstName'] ?></td>
<td><?php echo $row['LastName'] ?></td>
<td><?php echo $row['UserName'] ?></td>
<td><?php echo $row['Email'] ?></td>
<td><?php echo $row['ApplicationID'];?></td>
<td><a href="acceptApplication.php" class="btn btn-sm round btn-outline-success" name="acceptButton">Accept</button>
<a href="rejectApplication.php" class="btn btn-sm round btn-outline-danger" name="rejectButton">Reject</button></td>
</tr>
<?php
array_push($applyID, $row['ApplicationID']); }; ?>
</td>
<?php $convApplyID = implode(" ",$applyID); $_SESSION['applicants'] = $convApplyID; echo $convApplyID; // testing yung echo, remove in final code
?>
</tr>
</tbody>
Is there a way to get the values of the record that the button aligns with?
2
Answers
You have some syntax errors in there – your buttons start with
<a>
and end in</button>
which is going to complicate things.You may not need to use session data – instead, what I would do is make the buttons both
<a>
tags and add the data to thehref
attribute as GET params:This will make the data available on the next page in the
$_GET
var.The data will be visible in the address bar and on hover-over, though, so if you need the data to be non-public, then you can wrap each row in a
form
and use POST to submit the data to the next page as an action which gets you$_POST
variables on the other page.See also:
Your buttons don’t pass any information with them. The acceptApplication or rejectApplication scripts need to know which application they are processing. The simplest way is to pass a GET request. It should look like this:
If you don’t want to use a GET request (as they are stored publicly in the URL) you can use a form to pass a POST request to the respective script. That should look like this:
Another way you can pass your request securely is using AJAX. That would look something like this:
HTML: