I had a look at checkbox value 0 or 1 but am a little confused.
In the DB, columns hosting, complete_setup, and legal_compliant are either 1 or 0, if the user checks the checkbox, it updates it to 1:
<div class="form-group <?php echo (!empty($complete_setup_err)) ? 'has-error' : ''; ?>">
<label>Complete Setup</label>
<input type="checkbox" class="form-control" name="complete_setup" value="1" />
<span class="help-block"><?php echo $complete_setup_err; ?></span>
</div>
But what I would like, is if in the db it is already 1 the the checkbox to already be checked and if it is unchecked it to update the DB to 0. How do I make it return the value 0?
Thank you!
The issue is that not checked checkboxes are not at all sent to the server. That means that if check, then the variable is part of the posted values, if not checked it is not sent. You could modify that behavior on the client side, but it is much easier to simply evaluate the given information on the receiving server side:
You can evaluate whether the variable is present in the posted variables:
$isChecked = (isset($_POST['complete_setup']) && $_POST['complete_setup'] == 1) ? 1 : 0;
To control if the checkbox is already checked when you hand out the form requires to actively mark the checkbox as checked:
<?php $isChecked = ($row['complete_setup'] == 1) ? 'checked' : ''; ?>
<input type="checkbox" class="form-control" name="complete_setup" value="1" checked="<?php echo $isChecked ?>"/>
This assumes that you read the content of the database table into some array $row which should then contain the value 1 in the array element complete_setup.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With