Issue
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?
Solution
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:
<tbody>
<?php
while($row = mysqli_fetch_assoc($applicationResult)) {
?>
<tr>
<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?id=<?php echo $row['ApplicationID']; ?>" class="btn btn-sm round btn-outline-success">Accept</a>
<a href="rejectApplication.php?id=<?php echo $row['ApplicationID']; ?>" class="btn btn-sm round btn-outline-danger">Reject</a>
</td>
</tr>
<?php
}
?>
</tbody>
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:
<tbody>
<?php
while($row = mysqli_fetch_assoc($applicationResult)) {
?>
<tr>
<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>
<form action="acceptApplication.php" method="post">
<input type="hidden" name="applicationId" value="<?php echo $row['ApplicationID']; ?>">
<input type="submit" class="btn btn-sm round btn-outline-success" value="Accept">
</form>
<form action="rejectApplication.php" method="post">
<input type="hidden" name="applicationId" value="<?php echo $row['ApplicationID']; ?>">
<input type="submit" class="btn btn-sm round btn-outline-danger" value="Reject">
</form>
</td>
</tr>
<?php
}
?>
</tbody>
Another way you can pass your request securely is using AJAX. That would look something like this:
HTML:
<tbody>
<?php
while($row = mysqli_fetch_assoc($applicationResult)) {
?>
<tr>
<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>
<button onclick="processApplication(<?php echo $row['ApplicationID']; ?>, 'accept')" class="btn btn-sm round btn-outline-success">Accept</button>
<button onclick="processApplication(<?php echo $row['ApplicationID']; ?>, 'reject')" class="btn btn-sm round btn-outline-danger">Reject</button>
</td>
</tr>
<?php
}
?>
</tbody>
<script>
function processApplication(applicationId, action) {
var xhr = new XMLHttpRequest();
xhr.open("POST", 'processApplication.php', true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
// Handle the response here
console.log(this.responseText);
}
}
xhr.send("applicationId=" + applicationId + "&action=" + action);
}
</script>
Answered By - Jacob
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.