Re: HTML_QuickForm, advcheckbox, setChecked()

From: Date: Sun, 18 Apr 2004 16:53:59 +0000
Subject: Re: HTML_QuickForm, advcheckbox, setChecked()
References: 1 2 3  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-27933@lists.php.net to get a copy of this message
Hi, Aidan,
-----quote----- In the past, when I have needed behavior similar to the advanced checkbox, I have used a dual hidden + checkbox approach, instead of a JavaScript approach. For example...
     <input type="hidden"   name="my_checkbox" value="not-checked" />
     <input type="checkbox" name="my_checkbox" value="checked" />
... so that the 'not-checked' value comes back if the box is not checked, and if it is, the 'checked' value comes back instead (it overrides the earlier value). Is this a valid and standards-compatible approach? I ask only because HTML_QuickForm might be able to make use of it, instead of the JavaScript, thus tightening things up just a little bit. Just asking about practices; not a critique, the current behavior is just fine. ----- end quote ------- This seems a wacky thing to do, it's definitly not standards compatible - two elements may not have the same name.
I'm not certain that is the case. Each element must have a unique ID attribute, but multiple elements are allowed to have identical names (e.g., a series of radio buttons will use the same name). Perhaps I have missed something in the XHTML specification?
With checkboxes, to make a box checked it's: <input ... checked="checked" /> Not checked is: <input .. />. The use of value="not-checked" is completely invalid. The use of value="checked" doesn't work for me in firefox.
Allow me to clarify. I used the values "not-checked" and "checked" to indicate the values to be returned to the script, not to indicate the checked state as displayed in the browser. It could have just as well have read as "n" and "y", or "0" and "1". You are correct that this will not set the state; you properly note that "checked='checked'" will do so.
The way I (and you may have a reason this won't work for you) test the value of a checkbox is as follows: if ($_POST['checkbox'] == "on")
    $value = true;
else
    $value = false;
What is wrong with this simple approach?
Nothing at all. :-) However, because I am extra lazy, I find it easier to automatically post the proper value with the method I stated above (dual hidden + checkbox). If a box is not checked, the hidden value with the same name as the checkbox is posted; if a box is checked, then the value of the checkbox is posted. HTML_QuickForm guys, does this strike you as a reasonable alternative to the JavaScript process you use? Again, nothing wrong with QuickForm, it's just an illustration of a different practice. Thanks. -- Paul M. Jones pmjones@ciaweb.net Savant: the simple alternative to Smarty. http://phpsavant.com/

« previous php.pear.dev (#27933) next »