PHP and Check Boxes

Discussion in 'Web Design and Development' started by Aranince, Mar 1, 2008.

  1. Aranince macrumors 65816

    Joined:
    Apr 18, 2007
    Location:
    California
    #1
    For some reason...my PHP code does not like checkboxes.

    Code:
    	// Update the site settings
    	public function settingsAction()
    	{
    		$this->validate();
    		if( $this->_request->isPost() )
    		{
    			if( isset($_POST['enable']) )
    			{
    				$this->_db->fetchAll( "UPDATE settings SET setting_value='true' WHERE setting_name='site_enabled' LIMIT 1" );
    			}
    			else
    			{
    				$this->_db->fetchAll( "UPDATE settings SET setting_value='false' WHERE setting_name='site_enabled' LIMIT 1" );			
    			}
    
    		}
    		$this->_redirect('/admin');
    	}
    That code will only set it to false, and not true...no matter the status of the checkbox.

    Code:
    <sub>Enable Site: <input type="checkbox" name="enable" checked="checked" /></sub>
     
  2. Coolnat2004 macrumors 6502

    Coolnat2004

    Joined:
    Jan 12, 2005
    #2
    I never liked isset(). Try something like this:
    Code:
    if ($_POST['enable']=='1') $value = true;
    Although, I believe your code isn't working because the checkbox doesn't have a "value" attribute, so it is giving you an empty variable.
     
  3. EvanAgee macrumors member

    EvanAgee

    Joined:
    Aug 16, 2004
    Location:
    Indiana
    #3
    Exactly. Your checkbox needs to have a value or else isset() won't see it as being set.
     
  4. Aranince thread starter macrumors 65816

    Joined:
    Apr 18, 2007
    Location:
    California
    #4
    Ah..thanks, it works. Or I could just use empty()?
     

Share This Page