Update textInput Value from DropDownList

Please I got a problem on Yii2:

I want to insert value into a textInput field based on changes in the selected option from a dropdownlist. Here is my code:




<?= $form->field($model, 'course_taken')->dropDownList(ArrayHelper::map(Courses::find()->all(),'course_code','course_code')) ?>


<?= $form->field($model, 'course_details')->textInput(['maxlength' => true]) ?>



So when the user selects a course from the dropdownlist, the textInput will display the course details of the selected row. Both course_taken and course_details are columns from the same database table.

Thanks in advance.

please try this link


http://stackoverflow.com/questions/31064368/update-textinput-value-by-selecting-a-dropdown-list-in-yii-2

Thanks.

But that did not work. My case is slightly different. Any more help, please?

Hi foxychev

Try this,


<?= $form->field($model, 'course_taken')->dropDownList(

    ArrayHelper::map(Courses::find()->all(),'course_code','course_code'), 

    [

        'prompt'=>'--Select--',

        'onchange'=>'$("#model-course_details").val($("#model-course_taken option:selected").text());'

    ])

?>

<?= $form->field($model, 'course_details')->textInput(['maxlength' => true]) ?>

Tried it but nothing happened. The course_details textinput remained blank after making selection…




<?= $form->field($model, 'course_taken')->dropDownList(

    ArrayHelper::map(Courses::find()->all(),'course_code','course_code'), 

    [

        'prompt'=>'--Select--',

        'onchange'=>'$("#course_details").val($("#course_taken option:selected").text());',

        'id'=>'course_taken',

    ])

?>

<?= $form->field($model, 'course_details')->textInput(['maxlength' => true, 'id'=>'course_details']) ?>

Set the field ID’s manually and check it.

Did that work? I’m in the same situation… I’ll try what Selvakumar suggests…

My case is kind of different how ever I tried this option to see the behavior and It didn’t work. I coded this:


<?=$form->field($ordenProducto, 'id_producto')->dropDownList(ArrayHelper::map($catProductos,'id','descripcion'),

             ['prompt'=>'-Choose a option-',

              'onchange'=>'if($(this).val() == 1){

                 $("#precio_unitario").val("333"); }'

              ]); ?>

    <?= $form->field($catProductos, 'precio_unitario')->textInput(['maxlength' => true, 'id'=>'#precio_unitario']) ?>

Is there something wrong?