I need to catch the event when the checkboxes have been pressed (they can be inserted as many as possible into the HTML table by a cycle), and put its value in the MySQL table. So the form itself (removed too much):

<form name="new_form" action="/admin/osvega_test" method="POST"> <input class="my_button" type="submit" name="upload_submit" value="save" style="width: 200px"/> <table style="width: 1000;" id="activeTable" class="editableTable" overflow = "scroll"> <thead class="t_hdr"> <tr class="header"> <td><b>ID</b></td> <td><b>Печать</b></td> </tr> </thead> <tbody> <?php $i = 0; while ($row = $Result->fetch_assoc()) { $i++; ?> <tr id="z_<?=$i?>"> <td name = "post_id"><?=$row['id']?></td> <td> <input type="checkbox" name="stamp[<?=$row['id']?>]" value="<?=$row['stamp']?>" <? if($row['stamp'] == 1)echo $check ?> id="chkbox" > </td> </tr> <? } ?> </tbody> </table> </form> 

This is what the handler code looks like:

 <?php foreach ($_POST['stamp'] as $id => $value) { if (isset($_POST['stamp'])){ $value = 1; $Database->query("UPDATE `izgotoviteli` SET stamp = $value WHERE id = $id"); } else{ $value = 0; $Database->query("UPDATE `izgotoviteli` SET stamp = $value WHERE id = $id"); } } ?> 

That is, if I tick and press the submit button, they are saved in the database with the value 1 (tinyint (1)), everything works here. It is necessary that the removal of a tick is also tracked (although it should) and 0 is entered into the database when it is removed.

  • Ajax to help you. - teran
  • I thought about it, but in javascript is not strong. You can do it with PHP? - Alex
  • one
    without js you do not track the moment of unchecking the box. - teran

2 answers 2

First, you look at the $ _POST ['stamp'] array, and not at the value of the $ _POST [[stamp]] array element [$ id]

 if (isset($_POST['stamp'])) надо if (empty($_POST['stamp'][$id])) 

The isset () function will still return TRUE even if $ _POST ['stamp'] [$ id] = 0, so I replaced it with empty ()

Try this:

 if (empty($_POST['stamp'][$id])) { $value=0; } else { $value=1; } 

Like so

    And if so: in the body of the document, listen to clicking on the checkbox form, when the user unchecks it - value = 0 , if it checks again - value = data-value (new checkbox attribute, just duplicating the value)

     document.forms.new_form.addEventListener('click', function(event) { if (event.target.type = 'checkbox') { if (!event.target.checked) { event.target.value = 0; } else { event.target.value = event.target.dataset.value; } } console.log(event.target.value); }); 
     <form name="new_form" action="/admin/osvega_test" method="POST"> <input type="checkbox" name="stamp[1]" data-value="73457" value="73457" checked id="chkbox" /> <input type="checkbox" name="stamp[2]" data-value="90356" value="90356" checked id="chkbox2" /> </form> 

    ZY edit the handler, you have some kind of "not such":

     if (isset($_POST['stamp'])) { foreach ($_POST['stamp'] as $id => $value) { $Database->query("UPDATE `izgotoviteli` SET stamp = $value WHERE id"); } }