vendredi 29 avril 2022

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 chekbox given it's state in the database. What I want to do, and don't exactly know how to, is to be able for the user to check/uncheck a checkbox and inmediatly update the database with this new state, without having to press a submit button. 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 yet the code that will update values on the database, but I'll figure that out.

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

I want to know how could I retrieve the value of the checkbox of every row, and it's ID so I can update it on the database. Thanks for any help.




Aucun commentaire:

Enregistrer un commentaire