skip to Main Content

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


  1. 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 the href attribute as GET params:

    acceptApplication.php?key=value&key2=value2
    

    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:

    Login or Signup to reply.
  2. 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>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search