views:

390

answers:

3

Hello, 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.

A: 

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']; ?>
Mathias Bynens
A: 
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.

Goran Jurić
good, i have already doing this 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
Khurram Ali
Are you saying that it returns the last value (2) even if you select 0 or 1?If you mean that the value of the radio element is a single value that is normal. Radio element allows you to only pick one element to be returned. If you need to get the list of all the possible values you will have to use the checkbox element.I have updated my answer with a working code.
Goran Jurić
A: 

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.

pavan