For Programmers: Free Programming Magazines  


Home > Archive > PHP Pear > May 2007 > Re: [PEAR] Problem with quickform checkbox









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [PEAR] Problem with quickform checkbox
David Sanders

2007-05-22, 7:02 pm

Eugen Stoianovici wrote:
> I have a form with a few checkboxes used for updating some entrys. The
> problem is that if i use checkbox::setChecked(true), the checkbox
> remains checked even if the user unchecks it
>
> here's the code:
>
> $form = new
> HTML_QuickForm('events','POST',$_SERVER[
REQUEST_URI],NULL,true);
>
> $form->addElement('text', 'ca', 'CA:');
> $rmodem =& $form->addElement('checkbox', 'rmodem', 'Reset modem:');
> $rsoft =& $form->addElement('checkbox', 'rsoft', 'Reset soft:');
> $rhard =& $form->addElement('checkbox', 'rhard', 'Reset hard:');
> $form->addElement('text', 'phone, 'phone:');
>
>
> if($def["rhard"]>0)
> $rhard->setChecked(true);
> if($def["rmodem"]>0)
> $rmodem->setChecked(true);
> if($def["rsoft"]>0)
> $rsoft->setChecked(true);
>
> if($form->validate()){
> $form->freeze();
> savedata($form);
> }
>
> echo $form->toHtml();
>
> If rhard is checked in the inital display and the user unchecks it,
> form->freeze still sees it checked
> what am i doing wrong?
>


Normally you would set the *defaults* of the form so that if the user changes
the form, the defaults are overridden. The setChecked() method ensures that the
box is checked. Depending on the defaults and user input, setChecked is called
with the appropriate value.

Try this:

$form->setDefaults(array(
'rmodem' => ($def['rmodem'] > 0),
'rsoft' => ($def['rsoft'] > 0),
'rhard' => ($def['rhard'] > 0),
));

--
David Sanders
shangxiao
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com