简体   繁体   English

PHP:使用每行的复选框值更新数据库

[英]PHP: Update database with checkbox values for each row

I'm currently working on displaying a table containing a list of alarms, where each row contains a checkbox that determines whether the user has already viewed that alarm or not.我目前正在显示一个包含警报列表的表格,其中每一行都包含一个复选框,用于确定用户是否已经查看过该警报。 Here's an image :这是一张图片

So far, I managed to check/uncheck the checkbox given its state in the database.到目前为止,我设法选中/取消选中数据库中给出的 state 的复选框。 What I want to do, and don't exactly know how to, allow the user to check/uncheck a checkbox and immediately update the database with this new state without pressing a submit button.我想做的,但不知道如何做,允许用户选中/取消选中复选框并立即使用这个新的 state 更新数据库,而无需按下提交按钮。 The code that I have right now is the following.我现在拥有的代码如下。

<div class="card-body table-responsive p-0">
  <form action=modules/alarms.php method="POST">
    <table class="table table-bordered table-hover">
      <thead>
        <tr>
          <th>ID</th>
          <th>Tipus</th>
          <th>Data</th>
          <th>Vista</th>
        </tr>
      </thead>
       <tbody> 
         <?php 
           foreach ($result->fetchAll() as $row){
             if ($row[4] == 1){
                 $status = "checked";
             }else{
                 $status = "";
             }
            echo 
             '<tr data-widget="expandable-table" aria-expanded="false">
                <td>'.$row[0].'</td> 
                <td>'.$row[1].'</td> 
                <td>'.$row[2].'</td>
                 <td><input type="checkbox" name="chk1[]" value="viewed" '.$status.'></td>
               </tr>
               <tr class="expandable-body">
                 <td colspan="5">
                    <p>
                      <video width="416" height="416" controls>
                       <!-- el 42 es la cabecera C:/... fins videos-->
                       <source src="'.substr($row[3], 42).'" type="video/mp4">
                       Your browser does not support the video tag.
                      </video>
                     </p>
                  </td>
                </tr>';
                      }                      
       ?>                                                                         
  </tbody>
</table>

And the PHP code I have (I don't have the code that will update values on the database, but I'll figure that out. I want to know how I could retrieve the value of the checkbox of every row and its ID so I can update it on the database.我有 PHP 代码(我没有更新数据库值的代码,但我会弄清楚的。我想知道如何检索每一行的复选框的值及其 ID,所以我可以在数据库上更新它。

$ei = $_POST['chk1'];
if ($_POST["chk1"] == "chk1") {
    for ($i = 0; $i < sizeof($checkbox1); $i++) {
        print_r($checkboxl[$i]);
    }
}

To solve this problem, first I had to add some fields to the checkbox input.为了解决这个问题,首先我必须在复选框输入中添加一些字段。 WIth those changes, the table is generated as it follows:通过这些更改,生成的表如下所示:

<tr data-widget="expandable-table" aria-expanded="false">
    <td>'.$row[0].'</td> 
    <td>'.$row[1].'</td> 
    <td>'.$row[2].'</td>
    <td><input type="checkbox" name="id" id="viewed" value="'.$row[0].'" '.$status.'></td>
</tr>

After this is done, simply add a script that will retrieve the values from the clicked checkbox and pass them to the necessary PHP function:完成后,只需添加一个脚本,该脚本将从单击的复选框中检索值并将它们传递给必要的 PHP function:

$("input").change(function() {
if ($(this).is(':checked')){
  var viewed=1;
}else{
  var viewed = 0;
}
var id = $(this).val();

$.ajax({
         url:"modules/alarms.php",
         method:"POST",
         data:{viewed:viewed,id:id,},
         success: function(data){
        },
   });

Please note that the 'url' field is the relative path where our PHP function is implemented.请注意'url'字段是我们的PHP function实现的相对路径。

And now, simply update the database with the checkbox value (I'm using PDO objects) as it follows:现在,只需使用复选框值更新数据库(我使用的是 PDO 对象),如下所示:

    <?php 
  if(isset($_POST["viewed"])) {
    $id = $_POST["id"];
    $viewed = $_POST["viewed"];

    $sql="UPDATE `alarms` SET `viewed` = '$viewed' WHERE (`id` = '$id')";
    if($connection->query($sql) === TRUE){
      echo "Success";
      } else {
        echo "error" . $sql . "<br>".$connection->error;
      }}?>

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

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