Re: Determining submitted checkbox values in QuickForm
| From: | Justin Patrin | Date: | Wed, 18 May 2005 18:03:24 +0000 |
| Subject: | Re: Determining submitted checkbox values in QuickForm | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-19264@lists.php.net to get a copy of this message | ||
On 5/18/05, Skyler Slade <jsslade@gmail.com> wrote:
> Hi all,
> I've run into some confusion on handling checkbox elements in
> QuickForm. It is rather easy to create a list of checkboxes by using a
> standard loop around an SQL query, however during the form processing,
> I'm having difficulty determining which checkboxes were checked.
>
> Using the standard QuickForm 'checkbox' element, the checkbox values
> are always 1, and they cannot be changed. If one was to write a form
> such as this using HTML (instead of using the QuickForm class) finding
> out which checkboxes were checked would be relatively easy. If I were
> to write it in HTML, all of the checkboxes name's would be an array,
> such as array[], and the values for the checkboxes would be whatever I
> would give them. After form submission and during processing, I simply
> create a loop around $_POST['array'], and foreach value in array[], I
> do _something_ with the value. This makes it very easy to insert only
> selected items into a database.
>
> As valuable a feature as this would be (the ability to change checkbox
> values), it does not appear to be implemented so far as I can tell. In
> this case, how would one determine and parse which checkboxes were
> checked? As is, if I loop through the array of the checkbox names
> (using array[] for their names when creating them) I receive multiple
> values of 1, which isn't very helpful for determining which checkbox
> they belong to. There is a method called "getChecked()," would this be
> used? I realize that 'advcheckbox' may be able to help with this, but
> it seems too much like a complex solution to a simple problem, as I'd
> rather not introduce JavaScript into this form for various reasons.
Method1: put a key in the array in the name.
for($i = 0; $i < 5; ++$i) {
$form->addElement('checkbox', 'array[',$i.']');
}
foreach ($_REQUEST['array'] as $num => $value) {
if ($value) {
echo $num.' checked<br/>';
}
}
And I'm pretty sure you *can* set values. Have you tried setValue() on
the checkbox? $check->updateAttributes(array('value' => $value))?
$form->setDefaults()?
--
Justin Patrin