download2014711 2014-06-01 03:48
浏览 18

zf2选择框发送邮件中的代码和值

I am working with ZF2 Select boxes

I have set the options from the controller and everything is working fine. I choose the option and press select, the code goes to the action in the post values.

I want a little bit more; along with the code I want to pass the value too.

example: I have the following select options

The select box is for choosing the gender of the user; The corresponding action is "AddUserProfileForm" on confirming the action called is "AddUserProfile"

<select name="user_gender_code">
<option value="M">Male</option>
<option value="F">Female</option>
</select>

I want to pass both the code "M" and the corresponding value "Male" across the post This will ensure that in the subsequent form I have the Code to write to the Database and the Value to Disply on the screen without performing a database read.

The only option I can think of is to build the code as below

<select name="user_gender_code">
<option value="M~Male">Male</option>
<option value="F~Female">Female</option>
</select>

This will ensure that "M~Male" is sent across a post and then I split the value "M" & "Male" separately.

I am not setting the valueOptions in the form; In the form they are empty, I use a function in the controller to read the database and set the valueOptions;

Does someone have a better option; I can create a hidden form element "user_gender_name" and if this is populate with the value of the option selected before the post it will make my job easier and more clean

Does anyone know how to do this? Thank you in advance for your time

AddUserProfileForm - Code

    $this->viewModel = $this->getViewModel( );
    $EnvironmentTable = $this->getServiceLocator ( )->get( 'UserTable' );
    $post = $this->request->getPost( );
    //Get Route Variable Section
    //  ****Get Any Route Variable you need over here


    //End Get Route Variable Section

    $form = $this->getServiceLocator ( )->get( 'AddUserProfileForm' );
    //Get Table Records Section
    //  ****Get Any Table Records you need over here


    //End Get Table Records Section

    $form->get( 'user_gender' )->setValueOptions( $this->getLookupGenderList( ) );
    $this->viewModel->AddUserProfileForm = $form;
    //Set View Variables Section
    //  ****Define other view variables over here


    //End Set View Variables Section

    return $this->viewModel;

AddUserProfile - Code

$this->viewModel = $this->getViewModel( );
$post = $this->request->getPost( );
//Get Route Variable Section
//  ****Get Any Route Variable you need over here

//End Get Route Variable Section

$fromForm = $this->getServiceLocator ( )->get( 'AddUserProfileForm' );
//Bind Form Section
//  ****Bind the form with the Table Record over here

//End Bind Form Section
  • 写回答

2条回答 默认 最新

  • dongzhan7909 2014-06-01 05:11
    关注

    Unless I'm completely misunderstanding the question, your select element already contains the options as an array of key/value pairs, you only need to get the selected value, and then use it as key in the options array which will give you the name of the selected option ...

    if ($form->isValid()) {
        // get the element
        $selectElement = $form->get('selectElement');
        // get the selected value
        $selectValue   = $selectElement->getValue();  // 'M'
        // get the select options
        $selectOptions = $selectElement->getValueOptions(); // array('M' => 'Male, 'F' => 'Female');
        // get option name using selected value as key in options array 
        $genderName = $selectOptions[$selectValue];  // 'Male'
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)