2

I have a dropdown list in my view and would like to send an email later from controller

create view:

echo $form->field($model, 'company')->dropDownList(['a' => 'LG', 'b' => 'Samsung'], ['prompt'=>''])->label('Company');

action code:

public function actionVacancy()
{
    $model=new VacancyForm;

    if($model->load(Yii::$app->request->post()) &&$model->validate())
    {

      Yii::$app->mailer->compose('vacancy',[
                        ...
                        'company'=> $model->company,
                        ...
      ])

What is the proper way to access dropdown list selected value? Currently I can only access key, but don't understand how to access value

Thanks in advance

1
  • 2
    In your controller action put print_r($_POST);exit() then when you submit a form you'll see the value in the post array and know how to access it. Commented Oct 15, 2015 at 18:43

2 Answers 2

3

In your controller you need to load the POST values into the model before you can access them.

$model = new Model();

if ($model->load(Yii::$app->request->post())) {

//send email
$company = $model->company

...
Sign up to request clarification or add additional context in comments.

2 Comments

POST values are loaded.the problem is that $model->company returns 'a' and I don't know how to get 'LG' e.g. Anyway thanks for your answer.
LG won't be sent in the form, it's a key => value and only the key is sent, if you want LG from a you'll either have to set up a relation for it or set LG to be your key as well as your value. Your relation would be something like $model->company->lg_table. Good link on it blog.hashsolutions.in/technology/…
1

In the controller you obtain the result of the POST. In this case you get the value of the id related with your dropdown because this is value send by post.

If you want obtain the description in controller yon need find the descriptio related to the model by id.

 use yourapp\models\Company;

 $company = Company::find()
->where(['id' => $model->company])
->one();

assuming $ model->company contains the id of the istance you are looking for

  $company->name  //  

should contain the desired value.

1 Comment

Thanks a lot for your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.