Get radio button value on form post in Zend Form
i have an element of type Radio in zend form . how can i get value of radio button on form post.
keep in mind i am using Zend Form
if ($this->_request->isPost()) {
if ($form->isValid($_POST)) {
$values = $form->getValues();
}
}
i am already doing the above code it is really correct way to get values of posted elements but for radio button it only posts the last value of that radio button. for example: my radio button is like
$type = array("0"=>"0", "1"=>"1", "2"=>"2")
// add Order element
$order = $this->CreateElement('Radio', 'order');
$order->setMultiOptions( $type );
$elements[] = $order;
then after post it always returns : order = 2
On the other hand, if i use the same code while json = False like:
// ------------------------------------------------
/开发者_运维知识库/ prepare the form for ProductInfo
// ------------------------------------------------
$productinfo = new Form_ProductInfo();
$this->view->productinfo = $productinfo;
$this->view->productinfo->setAction($this->view->url());
$this->view->jsonEncoded = false;
then it works fine & pick selected radio button value.
By Using Zend_Form Just check your radio button name by viewing source code of your page and replace it in Controller file,if u want to set the names of your input control then go throw helper class.
if ($this->_request->isPost()) {
if ($form->isValid($_POST)) {
$values = $form->getValues();
}
}
You can than access the value of your form element from the $value variable.
$vale['name-of-the-radio-button-element']
EDITED
For the example you have provided in the comment:
$form = new Zend_Form();
$type = array("0"=>"0", "1"=>"1", "2"=>"2");
// add Order element
$order = $form->createElement('Radio', 'order');
$order->setMultiOptions( $type );
$form->addElement($order);
$form->addElement(new Zend_Form_Element_Submit('submit'));
echo $form;
if ($this->_request->isPost()) {
if ($form->isValid($_POST)) {
$values = $form->getValues();
var_dump($values['order']);
}
}
This will dump the selected value of the $order element.
If you have the following HTML:
<input type="radio" name="radioButtonName" value="someValue">
You can use the following PHP (plain PHP, not Zend Framework-specific) code in the page that handles the POST
request:
<?php echo $_POST['radioButtonName']; ?>
精彩评论