Checkbox Form Value

Hallo there,

i’ve built a form. I can get every value out of every input. But on checkboxes i can’t get no value out of it.

<input type="checkbox" name="agree" id="agree" value="checked" <?= $data['agree'] === 'checked' ? 'checked="checked"' : null ?> required/>

I will get a “Trying to access array offset on value of type bool” error and the debugger shows this line of code. This is based on a older forum post.

Thank you for your help!

If the checkbox is not checked, it will not appear in the $_POST or $_GET global variable. You can check with empty() or isset() like:

<input type="checkbox" 
       name="agree" 
       id="agree" 
       value="checked" 
       <?= isset($data['agree']) ? 'checked="checked"' : '' ?> 
       required>

Thank you! Now its working.