0

I have a table nested within a form that looks as follows:

enter image description here

The "leave_request_processor.php" file that receives the post request from the above file then reads as follows:

$approved = "Approved";

$rejected = "Rejected";

foreach($all_leave_ids as $leave_id){

    if(isset($_POST["approve"])){

    $strQuery2 = "UPDATE leave_applications SET status = '$approved' WHERE application_id = '$leave_id' ";

    $result2 = mysqli_query($connection,$strQuery2) or Exit ("Query execution failed");

    mysqli_close($connection);

    header("Location: leave_check.php"); /* Redirect browser */

    exit();

        }

    else if (isset($_POST["reject"])){

    $strQuery3 = "UPDATE leave_applications SET status = '$rejected' WHERE application_id = '$leave_id' ";

    $result3 = mysqli_query($connection,$strQuery3) or Exit ("Query execution failed");

    mysqli_close($connection);

    header("Location: leave_check.php"); /* Redirect browser */

    exit();

    }

   }

The code works but starting with the first row (whichever rows' button you click) in descending order then the page reloads and you're left with the pending requests.

I bet there's a way you can code it (I'm thinking JavaScript) so that if I click "Approve" on row 23 it acts on that row. Anyone know how I can achieve this?

2
  • You can just use one form per row instead of one form for all rows. That's probably the most straight-forward. Commented Jun 29, 2017 at 3:35
  • Thanks @Rasclatt. I found a makeshift workaround here (stackoverflow.com/a/43286487/5925104) Commented Jul 1, 2017 at 2:57

1 Answer 1

0

@Rasclatt I tried this out. Is this what you had in mind? Didn't work though.

$strQuery = "SELECT * from leave_applications WHERE status = 'Pending' ";

$result = mysqli_query($connection, $strQuery) or Exit("Query execution failed");

if($result->num_rows>0){


    // output data of each row

echo "<table class='pure-table pure-table-bordered'>
<thead>
    <tr>
        <th>Application ID</th>
        <th>Student ID</th>
        <th>Email</th>
        <th>Days Requested</th>
        <th>Status</th>
        <th colspan='2'>ACTION TO TAKE</th>
    </tr>
</thead>";

while($row = $result->fetch_assoc()){

    array_push($allleaveids,$row["application_id"]);

    echo 
"<form action='leave_request_processor.php' method='post' target='_self' name='leave_check' class='pure-form pure-form-aligned'>
    <tr>
    <td>".$row["application_id"]."</td>
    <td>".$row["student_id"]."</td>
    <td>".$row["email"]."</td>
    <td>".$row["days_requested"]."</td>
    <td>".$row["status"]."</td>
    <td><button class='button-success pure-button' name='approve' type='submit'>Approve</button></td>
    <td><button class='button-error pure-button' name='reject' type='submit'>Reject</button></td>
    </tr>
    </form>";   
echo "</table>";
}   

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.