View Full Version : Checkboxes

07-28-2008, 12:28 AM
How do I get the value of a check box in php, as in, see if it is checked or not?

This is the html:
<input type="checkbox" id="form_n_screen" name="screen_chkbox_n"/

I am using the following code to see if the box is checked:

// validate the chkbox
$this->screen_chkbox_n = $request->getPost('screen_chkbox_n');

if (!this->screen_chkbox_n){
$this->addError('checkbox', 'Please select an option above');

screen_chkbox_n is always blank... any ideas?

07-28-2008, 02:12 AM
If a checkbox is set, the value of the post name will be set. If its unchecked, the value is unset. No idea what you're code does for $request->getPost, but I'd say the problem is that there is no such thing as this->screen_chkbox_n. $this->screen_chkbox_n is likely what you want, but allowing php to generate object properties on the fly is not a very good idea.

07-28-2008, 09:50 AM
You seem to have a missing $ sign on your if statement:

if (!$this->screen_chkbox_n){
$this->addError('checkbox', 'Please select an option above');

07-28-2008, 04:30 PM
Aha... that is most likely the problem. Thanks very much.