将功能绑定到按钮onclick循环

时间:2017-06-29 01:52:56

标签: javascript php

我有一个嵌套在表格中的表格,如下所示:

enter image description here

" leave_request_processor.php"接收来自上述文件的post请求的文件,如下所示:

$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();

    }

   }

代码可以正常工作,但是从第一行开始(按您点击的任何行'按钮)按降序排列,然后页面重新加载,并且您还有待处理的请求。

我敢打赌,您可以通过这种方式对其进行编码(我正在思考JavaScript),这样,如果我点击"批准"在第23行,它作用于该行。有谁知道我怎么能做到这一点?

1 个答案:

答案 0 :(得分:0)

@Rasclatt我试过这个。这是你的想法吗?虽然没有工作。

$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>";
}   

}