Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Checkboxes

  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    61
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Checkboxes

    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:

    PHP Code:
               // 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?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    eeijlar (07-28-2008)

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Location
    N. Ireland
    Posts
    351
    Thanks
    16
    Thanked 4 Times in 4 Posts
    You seem to have a missing $ sign on your if statement:
    Code:
    if (!$this->screen_chkbox_n){           
                    $this->addError('checkbox', 'Please select an option above');    
                }
    Daemonkin.
    If this was helpful, please add to my reputation
    Thousand Sons - Freelance Web Developer - ninetyonedegrees.com

  • Users who have thanked daemonkin for this post:

    eeijlar (07-28-2008)

  • #4
    New Coder
    Join Date
    Feb 2006
    Posts
    61
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Aha... that is most likely the problem. Thanks very much.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •