PHP process checkbox when already checked to uncheck

I have some issue, I make a form with checkbox.
When checkbox checked and press submit, it run PHP process to change value in database.

But I want to know how to change value in database when checkbox which already checked revert back to unchecked.

Here my code for my form

<form id=”formName” method=”POST”>
<?php
global $wpdb;
$results = $wpdb->get_results( “SELECT * FROM {$wpdb->prefix}faculty”, OBJECT );
foreach ($results as $result):
?>
<tr>
<td><?php echo $result->univ_name ?></td>
<td>
<input type=”checkbox” name=”txtCheck[]” value=”<?php echo $result->univ_code ?>” <?php if($result->upload_document == “yes”){ echo ‘checked’; } ?> /><br />
</td>
</tr>
<?php endforeach; ?>

</tbody>
</table>
<input type=”submit” name=”univSettingsSubmit” value=”Update” />
</form>

and this is my process

<?php

if(isset($_POST[‘txtCheck’])){
$ids = $_POST[‘txtCheck’];

foreach ($ids as $id){
$table = $wpdb->prefix . ‘faculty’;
$wpdb->update(
$table,
array(
‘upload_document’ => ‘yes’
),
array( ‘univ_code’ => $id )
);
}
}

?>

When checkbox unchecked the value in database back to “no”

Read more here:: PHP process checkbox when already checked to uncheck

Leave a Reply

Your email address will not be published. Required fields are marked *