How To Dynamically Create Text Field Depend On Some Dropdown List Option

I am currently working on a project in which I want to create a form just like in the link Form.

In the form which I am gonna create, most of the dropdown lists are fatched from lookup table.

My main question is that, when I select a a certian option from dropdown list then depend on that option the text fields related to that option are displayed, which will be filled.

Thanks in advance.

This is something that javascript can help you with (or jQuery javascript framework which comes along with Yii Framework).

I suggest you use an ajax call (in jQuery) to an action in one of your controllers, and this action will return the values you need to populate in your form.


 'onchange' => 'javascript:if ($(this).val() == "text" || $(this).val() == "textarea") {

$("#place-holder, #field-size").show();

 } else {

$("#place-holder, #field-size").hide();

  }'),

You could add an onchange event to the dropdown list. It might be painful to do this with a lot of items.

Pretty much if the value in the dropdown is equal to text or textarea it shows my form fields with id="place-holder" and id="fieldsize".

it is important to have an if else because you want to show it if the user selects if but hide it if the user selects a different dropdown option after the initial selection.

also you will want to make the hidden div / input etc style="display:none" by default.

i.e.


<div class="row" style="display: none" id="place-holder">

 	<?php echo $form->textField($model,'value'); ?>

</div>

or

<?php echo $form->textField($model,'value',array('id'=>'place-holder', 'style'=>'display:none'));?>