I'm trying to get value of selected dropdown menu from signup form. i need to store that value in db. dropdown code is.
<?= $form->field($model, 'sex')->dropDownList(['1' => 'Male', '2' => 'Female'],['prompt'=>'Select Gender...'] )?>
I have tried with property 'sex[]' here. but it didn't help.
i have tried in controller with post request.
$post=Yii::$app->request->post();
$chosen = Yii::$app->request->post('sex', '');
print_r($chosen);
print_r($post->sex);
echo "1:".$this->sex;
echo "<br>";
echo "2:".$this->sex;
echo "<br>";
echo "3:".$this->sex[0];
echo "<br>";
echo "4:".$this->sex[1];
echo "<br>";
but non of these printed the vlaue in post submitted. it contains the value for sex. here is the print_r($post) result [sex]=>1 but if is use'sex[]'in dropdown it shows something like [sex]=>array( [0]=>1)
if you want to select more than one, make sure that you create it as array in form Property sex[]
like this
<?= $form->field($model, 'sex[]')->dropDownList(['1' => 'Male', '2' => 'Female'],['prompt'=>'Select Gender...'] )?>
then load it to your model
$model->load(Yii::$app->request->post())
it would be
$model->sex
(that is an array).
if you want to select just one option
you have to had something like this
<?= $form->field($model, 'sex')->.... ?>
and your answer will be $model->sex (that is 1 or 2)