Re: HTML_QuickForm2, getting values of radio elements
| From: | Alexey Borzov | Date: | Mon, 12 Mar 2012 08:49:18 +0000 |
| Subject: | Re: HTML_QuickForm2, getting values of radio elements | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-31451@lists.php.net to get a copy of this message | ||
Hi Olaf,
On 11.03.2012 20:16, Olaf Gleba wrote:
i have a form with several elements, amongst some radios: // example type input text $name = $FORM->addElement('text', 'name', ...); // type radio $FORM->addElement('radio', 'test', array('value' => 1), array('content' => 'whatsoever')); $FORM->addElement('radio', 'test', array('value' => 2), array('content' => 'whatsoever')); I like to store the single element values in a new array. As i don't no how set a reference var for radio elements like i do on other elements, i fetch the value from the $_POST Array. $form_data = array( 'name' => $name->getValue(), 'test' => $_POST['test'] );Getting the values directly from superglobals kinda defeats the purpose of using QF2, you can get some unexpected stuff.
Is there a more API-like method or way to access radio element values? I am afraid i miss something in the docs/source examples.This should work: foreach ($FORM->getElementsByName('test') as $radio) {
if ($value = $radio->getValue()) {
break;
}
}
You can also put radios in a group:
$radioGrp = $FORM->addGroup();
$radioGrp->addElement('radio', 'test', array('value' => 1), array('content' => 'whatsoever'));
$radioGrp->addElement('radio', 'test', array('value' => 2), array('content' => 'whatsoever'));
// This will return something like array('test' => 1)
$value = $radioGrp->getValue();