Dropdown add new value when clicking on 'other' option

Hello all,
I am new to yii and I was trying to create a dropdown list with an ‘other’ option at last and when i click on it, a popup occurs of a text field and then i enter a value there and it gets added to the list,so that it shows the updated list.
How can i do this?

This is my form

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use yii\web\JsExpression;

/* @var $this yii\web\View /
@var $model app\models\Department /
@var $form yii\widgets\ActiveForm */

<?php $form = ActiveForm::begin(); ?>

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

 <?= $form->field($model, 'deptlocation')->dropDownList(
        ArrayHelper::map(\app\models\Location::find()->all(), 'loc', 'loc'), // Flat array ('id'=>'label')
        ['prompt' => 'Select',]) 

<div class="form-group">
    <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>

<?php ActiveForm::end(); ?>

Thank you

Hey did you get a solution to the above?

This description is to general, it describes what you want, but to do it you should split it in half.
1st task adding new location.
2nd task reload dropdownlist.

  1. I would create modal with form that has 1 input setup client side validations. Don’t render submit button, just you could set up jquery to trigger after pressing enter to validate field and send ajax with form fields (your input + csrf). On success you could handle 2nd task.

  2. You could reload page (then ajax event is no needed just regular post), if it’s not an option, you can use pjax wrap on your dropdown and trigger it afther success. Or just use regular jquery adding elements to dropdown

1 Like