简体   繁体   English

将功能绑定到循环中的onclick按钮

[英]Bind function to button onclick in a loop

I have a table nested within a form that looks as follows: 我有一个嵌套在表格中的表格,如下所示:

在此处输入图片说明

The "leave_request_processor.php" file that receives the post request from the above file then reads as follows: 从上述文件接收发布请求的“ leave_request_processor.php”文件如下所示:

$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. 我敢打赌,有一种方法可以对它进行编码(我在考虑JavaScript),这样,如果我在第23行单击“批准”,它将作用于该行。 Anyone know how I can achieve this? 有人知道我能做到这一点吗?

@Rasclatt I tried this out. @Rasclatt我尝试了这个。 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>";
}   

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM