【问题标题】:Bind function to button onclick in a loop将函数绑定到循环中的按钮 onclick
【发布时间】:2017-06-29 01:52:55
【问题描述】:

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

从上述文件接收到post请求的“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();

    }

   }

代码有效,但从第一行开始(无论您单击哪一行的按钮)按降序排列,然后页面重新加载,剩下的就是待处理的请求。

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

【问题讨论】:

  • 您可以每行只使用一个表单,而不是所有行都使用一个表单。这可能是最直接的。
  • 谢谢@Rasclatt。我在这里找到了一个临时解决方法 (stackoverflow.com/a/43286487/5925104)

标签: javascript php


【解决方案1】:

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-11
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    • 2015-04-16
    • 2017-11-14
    • 1970-01-01
    相关资源
    最近更新 更多