How do I make the checkbox have the value of the database id corresponding to the record that was marked?

Asked

Viewed 54 times

-1

< script >
  function verificaChecks() {}
var aChk = document.getElementsByName("verifica");
for (var i = 0; i < aChk.length; i++) {
  if (aChk[i].checked == true) {
    if (aChk[i].value == "<?php echo $check?>") {
      <?php
            $status= mysqli_query($conn,"UPDATE bloqueios SET  ESTADO = 'Desconsiderado' where id= $check");
  ?>
    }
  } else {
    <?php echo ("nenhum marcado"); ?>
    // CheckBox Não Marcado... Faça alguma outra coisa...
  }
}
} <
/script>
<?php
 $dbname = mysqli_select_db($conn,"bloqueio-cartao") or die("Não foi possível selecionar o Banco");
 // Exibe o bloqueio e a multa
 $sql2 = mysqli_query($conn,"SELECT * FROM bloqueios  WHERE NOME LIKE '%".$nome."%' ");
 $numRegistros2=mysqli_num_rows($sql2);
 if ($numRegistros2 !=0) {
   echo '<tr>'."<td>";
 //echo ("<button id='r'>Recurso</button></td>");
 echo '<td>';
 echo ("<button> Desconsiderar</button>").'</td>';
 echo "<td>". ("<input type='button'  value='Desconsiderar' onclick='verificaChecks();'></td>");
 echo "<td>". ("<button> imprimir </button>")."</td>"."</tr>";
		while ($bloqueio = mysqli_fetch_object($sql2)) {
    $check= $bloqueio->id;
    echo '<tr>'."<td>";
     echo  ("<input type='checkbox' id='verifica' name='checkbox' value='<?php echo $check;?>'/>"); echo '
  <td>'; echo $bloqueio->HORA. '-' .$bloqueio->LINHA. '-' .$check.'</td>'.'</tr>'; echo '
  <td></td>'; } } // Se não houver registros else { echo "Nenhum bloqueio foi encontrado com esse nome ".$nome.""; } ?>
  </table>
  </div>

inserir a descrição da imagem aqui

1 answer

0

document.getElementById('btnSaveInDb').onclick = function(){
   
   let checkboxs = document.getElementsByClassName('checkbox-class');
   
   let dataForm = [];
   
   for(let k = 0; k < checkboxs.length; k++){
       
       if(checkboxs[k].checked){
          
          dataForm.push({id:checkboxs[k].id, estado:'desconsiderado'});
          
       }
       
   }
   
   console.log(dataForm);
   
  $.post("demo_test_post.php",
  {
    data: {info:dataForm}
  },
  function(data, status){
    alert("Data: " + data + "\nStatus: " + status);
  });
   
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>

<label>
<input type="checkbox" id="my_id_1" class="checkbox-class"  />
Checkbox example 1 </label><br/>

<label>
<input type="checkbox" id="my_id_2" class="checkbox-class"  />
Checkbox example 2</label><br/>

<label>
<input type="checkbox" id="my_id_3" class="checkbox-class" />
</label>Checkbox example 3<br/>


<button id="btnSaveInDb">Salvar no Banco</button>

First: There is a mistake of web semântica, here:

<input type='checkbox' id='verifica' name='checkbox' value='<?php echo $check;?>'/>

For the id must be unique.

The ideal would be to do so:

<input type='checkbox' id='<?php echo $check; ?>' name='checkbox' value='<?php "1 ou 0";?>'/>

Or otherwise, like this:

<input type='checkbox' id='<?php echo $check; ?>' data-id="<?php echo $check; ?>" name='checkbox' value='<?php echo $check;?>'/>

Here the global attribute is used data-* .

This way, it is possible to do various types of manipulations, mainly, with javascript.

  • Thanks, but still not solved my problem =( I wanted to mark the checkbox, the same took the id of the corresponding record and thus record a status for this record as "disregarded" ....

  • You want to do this one by one? For example: click a checkbox and automatically already trigger an event and then save some information in the bank, or just show to the customer?

  • save at the bank

  • select some checkboxs, automatically picks up the ids. Then press the disregard button and save "disregarded" in the bank on those ids that are in check..

  • I will edit here as I understood.

  • each line containing the check gets an id on the bank, wanted the check to get that value...

  • OK I see... =)

  • I saw your post, I do not want to record the check id in the bank. No bank has the columns: id , student, benefit, Cpf , misuse, status. I wanted the check to get this bank id so I could add in the status column "disregarded" ...

  • wanted to leave it like this:

  • id=1 student=Bruna Cpf=0000000 usage= xxxxxx status= disregarded

  • but for now the state column fields are blank, I need to update there by taking the id

  • Like this? [ { "id": "my_id_2", "status": "disregarded" } ] [ { "id": "my_id_1", "status": "disregarded" }, { "id": "my_id_2", "status": "disregarded" }, { "id": "my_id_3", "status": " } ]

  • Yes, but I don’t need to put the id on the check manually you understand? pq are many uses and the check is in a loop... as soon as it generated the use check I took the bank id and this id recorded disregarded...

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.