Select and display dynamic value of dropdownlist

Hi

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;  


            


	&lt;?php                 


            &#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--') ); 


       


            ?&gt;


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

</div>

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.

Hey,

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

Example:




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

<select id="dropdown">

   <option>test</option>

   <option>test2</option>

</select>


<script type="text/javascript">

   $(document).ready(function(){

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

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

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

      });

   });

</script>



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.

Hey,

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:




<?php

Yii::app()->clientScript->registerCoreScript('jquery');

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:

[list=1]

[*]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.

[/list]

Thanks A lot again yiwi and bennouna for reply.

It really helps me a lot.