Good day to all, help solve this problem, how do I put the checkbox in the database depending on if it checks 1, if not then 0, but you need to put it in that line in the database where it was checked on the site, in general I deduce checkboxes

<?php while ($row=$result->fetch_assoc()) { echo "<tr class='bordertr'><td>".$i++."</td><td>"."<form class='formch'><input type='checkbox' name='{$row['id']}' class='checkord' value='{$row['tocorrob']}'><label for='check1'>не принят</label></form>"."</td>"</tr>"; } ?> 

Ajax request

 $('.checkord').on('change', function(){ var sendData = $(this).closest('.formch').serialize(); $.ajax({ url: 'formcheck.php', type: 'POST', data: sendData, success: function(data){ alert(data); // заменить на нужное } }); }); 

and the formcheck.php handler itself

 <?php require_once 'dbconnect.php'; isset($_POST['check']) ? $checked = 1:$checked = 0; $result = $conn -> query ("UPDATE vacanciestbl SET tocorrob='$checked' WHERE id ='$id'"); ?> 

    1 answer 1

    First, when creating a form, add hidden with a value of 0, the name is the same as that of the checkbox -c
    Secondly, you need to transfer $id from the form back to the application. You do not do this
    Approximately the form will be such

     <form name="form" class="formch"> <input type="hidden" name="id" value="<?php echo $id;?>" /> <input type="hidden" name="data" value="0" /> <input type="checkbox" name="data" value="1" /> </form> 

    After submitting this form, in the $_POST array you should have
    $_POST['id'] - key from the base, we will write to it
    $_POST['data'] - value 0 if not selected 1 if selected
    After that you can write data to the database.

    Approximate form output code

     <?php $i = 0;?> <?php while ($row = $result->fetch_assoc()) : ?> <tr class='bordertr'> <td><?php echo ++$i?></td> <td><form name="form" class="formch"> <input type="hidden" name="id" value="<?php echo $row['id'];?>" /> <input type="hidden" name="data" value="0" /> <input name="data" class="checkord" <?php echo (!empty($row['tocorrob']) ? 'checked="checked"' : '');?> type="checkbox" value="1" id="check-<?php echo $row['id'];?>" /> <label for='check-<?php echo $row['id'];?>'>не принят</label> </form> </tr> <?php endwhile; ?> 

    Handler

     <?php require_once 'dbconnect.php'; $id = $_POST['id']; $checked = $_POST['data']; $result = $conn->query ("UPDATE vacanciestbl SET tocorrob='$checked' WHERE id = '$id'"); ?> 

    There is not any filtering and screen adaptation, so be careful

    • My form is generated by php, the checkbox can be 5 and 50 pieces, add hidden for each one? - n0kk
    • @ n0kk You can of course hidden with name="data" release, but hidden with name="id" needed, otherwise you will not know what to insert in WHERE id ='$id' and accordingly which line you want to update - Farkhod Daniyarov
    • and in variable $ id to enter? I sit today almost the whole day already the head is boiling, and why name = "id"? - n0kk
    • Farkhod I understand so I write down the form? - n0kk
    • <form name = "form" class = "formch"> <input type = "hidden" name = "{$ row ['id']}" value = "{$ row ['tocorrob']}" /> <input type = "checkbox" name = "data" value = "1" /> </ form> - n0kk