Select and display dynamic value of dropdownlist


I am new to yii. I have used crud using gii. in _form.php page i am using dropdownlist it works fine code for that is

<div class="row">

	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'permission_id'); ?&gt;  



            &#036;models = Permissionentity::model()-&gt;findAll(array('order' =&gt; 'permission_id')); 

            &#036;list = CHtml::listData(&#036;models,'permission_id', 'permission_id');                  

            echo &#036;form-&gt;dropdownList(&#036;model,'permission_id',&#036;list,array('prompt'=&gt;'--Select--') ); 



	&lt;?php echo &#036;form-&gt;error(&#036;model,'permission_id'); ?&gt;


suppose after selecting the value in dropdownlist if i want to display in form like

"You have selected "<dropdown value>;

so how to dynamically select that single value of dropdownlist?

and should change according to option selected.

any idea?

thnx in advance.


you will need javascript to do that. I recommend to use jquery.


<div id="show_dropdown_content"></div>

<select id="dropdown">




<script type="text/javascript">


      $("#dropdown").change(function() {

         var content = $("#dropdown option:selected").text();

         $("#show_dropdown_content").text("You have selected: "+content);




Thanks a lot yiwi for reply.

but in yii crud should i write ajax-post array option in dropdown() to achieve that or something else ?

still i am confused how to achieve this in yii.


you can use your existing code for rendering the dropdown menu in html. Yii should give that dropdown automatically a unique id, so you can use that to replace the "#dropdown’ in jquery code. The ‘#show_dropdown_content’ must be added manually in your view.

Best practise to render your jquery code is to use CClientScript. You should take a look at the functions “registerScript” and “registerScriptFile”. So if you don’t want to move your javascript code in a external script file you can do:



Yii::app()->clientScript->registerScript('dropdown', '

   $("#dropdown").change(function() {

      var content = $("#dropdown option:selected").text();

      $("#show_dropdown_content").text("You have selected: "+content);




Simply add this code at the top of your view file. :slight_smile:

As side comments to yiwi answer:


[*]You don’t need to register jQuery: you’re probably using a CActiveForm in your view, so Yii already registers jQuery for you (as well as the yiiactiveform jQuery plugin). Moreover, registerScript when used with the default position registers itself jQuery.

[*]Instead of text(), I’d use val() to retrieve the selected option, and html() to set the div contents.


Thanks A lot again yiwi and bennouna for reply.

It really helps me a lot.