views:

409

answers:

3

Just wondering how it works and how to handle the information.

Let's say I have a form like this:

$multi = new Zend_Form_Element_Multiselect('users');
$multi->setMultiOptions(array(
    //'option value' => 'option label'
    '21' => 'John Doe',
    '22' => 'Joe Schmoe',
    '23' => 'Foobar Bazbat'
));
$form->addElement($multi);

If a user selects one, or multiple values from a multi-select box...

  • How do I get the value that the user selected?
  • Does it return in array? or what?
  • How can I tell how many items the user selected?
+1  A: 
$form->getElement('name')->getValue()

will return the value of $_POST['name']. You can make

$_POST['name']

be an array by defining the name of the element with brackets at the end. So in this case, 'name[]'. In the Zend Framework, use an element that extends

Zend_Form_Element_Multi

Please see: http://www.framework.zend.com/manual/en/zend.form.standardElements.html#zend.form.standardElements.multiselect

For example:

$multi = $form->createElement('multiselect', 'name[]');
$multi->setMultiOptions($options);
$form->addElement($multi);

if ($form->isValid($_POST)) {
    $userSelectedOptions = $form->getElement('name')->getValue();
}
Brad
+2  A: 

Using a multi select element like this one:

$multi = new Zend_Form_Element_Multiselect('users');
$multi->setMultiOptions(array(
    //'option value' => 'option label'
    '21' => 'John Doe',
    '22' => 'Joe Schmoe',
    '23' => 'Foobar Bazbat'
));
$form->addElement($multi);

You can get the values of the element like this:

public function indexAction()
{
    $form = new MyForm();

    $request = $this->getRequest();
    if ($request->isPost()) {

        if ($form->isValid($request->getPost())) {

            $values = $form->getValues();
            $users = $values['users']; //'users' is the element name
            var_dump $users;
        }
    }
    $this->view->form = $form;
}

$users will contain an array of the values that have been selected:

array(
    0 => '21',
    1 => '23'
)
Andrew
A: 

Hi how can we dissable some inactive contents listed inside the multiple select box..

eg ;- name1 name2 name3 name4

are the categories listed inside multiple select box. name1 and name4 are inactive so I have to show it as a non selectable content in my multiple select box.. how can I do that in zend..any body can help me.

nibohs
You should post this as your own question. You will get a better response.
Andrew