Re: QuickForm checkboxes and their return values

From: Date: Fri, 19 Nov 2004 19:17:52 +0000
Subject: Re: QuickForm checkboxes and their return values
References: 1  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-15698@lists.php.net to get a copy of this message
On Fri, 19 Nov 2004 12:03:11 -0700, Mitch Anderson <mitch@metauser.net> wrote: > I'm running into problems in using quickform... I'm trying to > figure out how I check to know if the checkbox was checked. The problem > I'm having is, depending on the data in the database, the check should > be checked already. However, If I remove the check and submit... > exportValue is still sending a 1... If its not checked in > the DB, and I check it, and submit, it works fine. Or if its not > checked and I submit it unchecked, it comes through as being > unchecked. I've been trying to find an example of how someone else was > using it and reading through pear.php.net[1] hasn't shed any more light > on things either... A sample of my code is below. What am I doing > wrong? > > // $data_array holds result of one row from a database. > $data_array = $data_result->fetchRow(DB_FETCHMODE_ASSOC); > > $form = new HTML_QuickForm('Form'); > > $checked =& $form->addElement('checkbox', 'check1', NULL, > 'CheckMe', > null); > if($data_array['check1'] == 1) { $checked->setChecked(true); } Ummm....you're forcing the checkbox to be checked here if it's checked in the DB. You should only do this if the form hasn't been submitted. I.e. in the } else { section below. > $form->addElement('submit', null, 'Submit'); > > if($form->validate()){ > $checked = $form->exportValue('check1'); > > if($checked){ > $ret_array['check1'] = 1; > } else { > $ret_array['check1'] = 0; > } > } else { > $form->display(); > } > > [1] > http://pear.php.net/manual/en/package.html.html-quickform.html- > quickform-checkbox.html-quickform-checkbox.php > > Mitch > -- Justin Patrin

« previous php.pear.general (#15698) next »