dsdzvprlp51692469 2010-10-29 13:54
浏览 39
已采纳

Zend Multiselect Element仅发布一个选定值

I am creating multiple select element like this and it is showed successfully on form:

$element = new Zend_Form_Element_Multiselect('clinics');
$element->setLabel("Clinics");
$element->setAttrib( 'style','width: 240px' );
$element->setMultiOptions( array( '1'=>'clinic1', '2'=>'clinic2' ) );

After rendering above element it shows the following html in html source:

<select name="clinics[]" id="clinics" multiple="multiple" style="width: 240px" size="5" class="required" tabindex="41">
    <option value="1" label="clinic1">clinic1</option>
    <option value="2" label="clinic2">clinic2</option>
</select>

But when I submit the form with two selected fields and print_r the result like this:

    $request = $this->getRequest();
    $form = new Patient_Form_Patient( $formOptions );

    if ( $request->isPost() ) {
        if ( $form->isValid( $request->getPost() ) ) {
            $values = $form->getValues();
            print_r($values);die();
        }
    } 

It stores only first selected option in array but not all selected elements:

Array
( 
    [clinics] => Array
        (
            [0] => 1
        )

    [save] => Submit
)

Can someone help that how can I submit multiple values ?

  • 写回答

3条回答 默认 最新

  • douxi6903 2010-11-04 13:54
    关注

    I have reconstructed your problem and I got no such error. You can see what I did below:

    Application_Form_Patient

    class Application_Form_Patient extends Zend_Form
    {
    
      public function init()
      {
        $this->setName('patient');
    
        $element = new Zend_Form_Element_Multiselect('clinics');
        $element->setLabel("Clinics");
        $element->setAttrib( 'style','width: 240px' );
        $element->setMultiOptions( array('1'=>'clinic1', '2'=>'clinic2' ) );
    
        $submit = $this->createElement('submit', 'submit');
        $submit->setLabel('Submit');
    
        $this->addElements(array(
          $element, $submit
        ));
      }
    
    }
    

    IndexController.php

    class IndexController extends Zend_Controller
    {
    
      function indexAction()
      {
        require_once 'Application/Form/Patient.php';
        $form = new Application_Form_Patient();
    
        $request = $this->getRequest();
    
        if ( $request->isPost() ) {
          if ( $form->isValid( $request->getPost() ) ) {
            $values = $form->getValues();
            Zend_Debug::dump($values);
            die();
          }
        } 
    
        $this->view->form = $form;
      }
    
    }
    

    index.phtml

    <?php
    echo $this->form;
    

    here's the debug output (one selected item and two selected items)

    # select one item
    array(1) {
      ["clinics"] => array(1) {
        [0] => string(1) "1"
      }
    }
    
    # select two items
    array(1) {
      ["clinics"] => array(2) {
        [0] => string(1) "1"
        [1] => string(1) "2"
      }
    }
    

    Hope it can help you ;)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功