1

How can I insert a value on a checkbox?

Do I need to use a script or how is this possible? Because when I am inserting value it will add, but if I update it, it is not updating 1 = checked and 0 = unchecked.

<input type="checkbox" id="checkbox1" name="checkbox1" value="0"
       <?= ($checkbox1 ?? null) == 1 ? 'checked' : '' ?>
>
hakre
  • 193,403
  • 52
  • 435
  • 836

3 Answers3

0
<?php $checkbox1 = isset($_REQUEST['checkbox1']) ? $_REQUEST['checkbox1'] : 0; ?>

<input type="checkbox" id="checkbox1" name="checkbox1" value="1" <?= $checkbox1 == 1  ? 'checked' : '' ?>>

You have set the value is 0, so on form submit, the request(post/get) variable will get 0 but you have checked it's 1 or not.

Kajal Pasha
  • 104
  • 3
  • 1
    The construct `$checkbox1 = $_REQUEST['checkbox1'] ?? 0;` is a bit more modern. Note that the value doesn't have to be 0 or 1, it can just be the name of the property you set with the checkbox. – KIKO Software Jul 27 '23 at 12:23
  • Sorry, using isset is better – Kajal Pasha Jul 27 '23 at 13:17
  • I wouldn't call it better, it's equivalent. It is fine if you prefer it, I can understand that. Or do you have real reasons to call it better? – KIKO Software Jul 27 '23 at 13:28
  • isset check it's defined or not, mmm, check by ?? may be smarter, – Kajal Pasha Jul 27 '23 at 13:39
  • Well, the [Null Coalescing Operator](https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.coalesce) is more modern and shorter. It takes some time to get used to, but once you do it is quite handy. – KIKO Software Jul 27 '23 at 13:42
0

A checkbox works a little differently than other inputs. A checkbox doesn't work by changing its value when it is checked or not. A checkbox either has a value when it is checked, or it doesn't have a value when it is not checked.

In HTML form submissions, only input elements that are considered "successful" are submitted with the form. For example, a disabled element is not considered "successful", so it will not be submitted.

In the case of a checkbox, a checkbox that is checked is considered "successful", but a checkbox that is unchecked is not considered "successful". Because of this, when checking the submitted form data in PHP, the element will only exist if the checkbox was checked.

So, if you define your input like this:

<input type="checkbox" id="checkbox1" name="checkbox1" value="1">

When the form is submitted, the request will include checkbox1=1 when the checkbox is checked, or the request will not include checkbox1 at all when the checkbox is not checked.

So, the final code you're looking for would be something like this:

<?php
// If the value exists in the request, it will be used. Otherwise, default to 0.
$cb1 = $_REQUEST['checkbox1'] ?? 0;
?>

<input type="checkbox" id="checkbox1" name="checkbox1" value="1" <?= $cb1 == 1 ? 'checked' : '' ?>>
patricus
  • 59,488
  • 15
  • 143
  • 145
-1

Have a look at input type checkbox to learn how checkboxes work.

When you submit a form the value of checked boxes is returned in $_GET["checkbox1"] or $_POST["checkbox1"] depending on the method you choose.

See: $_GET or $_POST or even $_REQUEST.

For a POST example see: Handling checkbox in a PHP form processor

KIKO Software
  • 15,283
  • 3
  • 18
  • 33
  • so that means i cant change its value? – Jonjon Candare Jul 27 '23 at 12:16
  • @JonjonCandare If you read the page to which I gave you the link, it says about the value: _"A string representing the value of the checkbox. This is not displayed on the client-side, but on the server this is the value given to the data submitted with the checkbox's name."_. – KIKO Software Jul 27 '23 at 12:20