Checkbox is always checked even not checked

Viewed 88

I have a checkbox that is always checked no matter if I checked the box or not.

Here is how the checkbox is currently setup:

    <input class="classname" id="check"  value="1" <?php echo (($temp['test']) ? 'checked' : '');  ?> name="check" type="checkbox" />

Here is how the check box value is being captured:

     $check = $_POST['check'] ?? 0;

I also tried the following code as well:

      $check = (isset($_POST['check']) == '1' ? '1' : '0');

I tried the hidden field approach as well and it didn't work. Any ideas on why the checkbox is showing marked even though I removed the check.

1 Answers

I think Drussy might be onto something, where does $_POST['test'] come from?

Try this code:

$checked = isset($_POST['check']) ? 'checked' : '';

<input class="classname" id="check"  value="1" <?php echo $checked; ?> name="check" type="checkbox" />
Related