Selecting From Dorpdownlist

How can I select from the dropdownlist? I have the following code but it is not working.


<?php 


echo CHtml::dropDownList('country_id','', array(1=>'USA',2=>'France',3=>'Japan'));

if ('#country_id' === 1)

echo "Thank You";


else if('#country_id' === 2)

	echo "merci beaucoup";

else

 echo "arigato";


?>

Hi,

This dropdown is not binded with your model. so its not selecting given value.

Please try to do the following

<?php echo $form->dropDownList($model,‘country_id’, array(1=>‘USA’,2=>‘France’,3=>‘Japan’)); ?>

Thanks

chandran nepolean

Is it necessary to bind it to a model? Suppose I don’t want to bind it to a model then what is the solution.

Hi,

Look at this link

http://www.yiiframework.com/doc/api/1.1/CHtml#dropDownList-detail

Second option is the value which is used for selection. But its empty in your code…so always choose first element

Thanks

chandran nepolean

can you give me small example because even after writing 2nd option it’s still not working.

Hi,

<?php echo CHtml::dropDownList(‘store’, ‘storeId’, CHtml::listData(Sitestore::model()->findAll(),‘storeId’, ‘storeName’), array(‘prompt’=>‘Select Store’)); ?>

If its not working…Please paste your html code after rendering. let me check which value is missing.

Thanks

chandran nepolean

Following is my changed code




echo CHtml::dropDownList('country_id','hello', array(1=>'USA',2=>'France',3=>'Japan'));

if ("hello" === '1') echo "Thank You";

else echo "arigato";



and following is my html code


<select name="country_id" id="country_id"> 

<option value="1">USA</option> 

<option value="2">France</option> 

<option value="3">Japan</option> </select>



dropdown list is working fine but I am not able to select from the dropdown. For example in my code it always show output as arigato even if I select USA from dropdown.

Hi,

<?php echo CHtml::dropDownList(‘country_id’,3, array(1=>‘USA’,2=>‘France’,3=>‘Japan’));?> … this is selecting Japan

<?php echo CHtml::dropDownList(‘country_id’,2, array(1=>‘USA’,2=>‘France’,3=>‘Japan’));?> … this is selecting France

<?php echo CHtml::dropDownList(‘country_id’,1, array(1=>‘USA’,2=>‘France’,3=>‘Japan’));?> … this is selecting USA

Now its your wish… i dont know how you will pass this value…

Thanks

chandran nepolean

Thanks for you reply. But I don’t think this will work since I have taken a simple example, what if their are 10 items in the list? The code will become too complicated. Is their any other method?

Hi,

Tell me one thing

From where are you getting selecting element ? from database or hardcoded

We have two options called selectbykey and selectbyname…which one did u prefer here?

can you change both key and value like this…for example

[color=#000000]array[/color]color=#666600[/color]

[color=#000000]echo [/color][color=#660066]CHtml[/color][color=#666600]::[/color][color=#000000]dropDownList[/color][color=#666600]([/color][color=#008800]‘country_id’[/color][color=#666600],[/color][color=#008800]‘hello’[/color][color=#666600],[/color][color=#000000] array[/color]color=#666600);[/color][color=#000000]

[/color][color=#000088]if[/color][color=#000000] [/color][color=#666600]([/color][color=#008800]“hello”[/color][color=#000000] [/color][color=#666600]===[/color][color=#000000] [/color][color=#008800]‘1’[/color][color=#666600])[/color][color=#000000] echo [/color][color=#008800]“Thank You”[/color][color=#666600];[/color][color=#000000]

[/color][color=#000088]else[/color][color=#000000] echo [/color][color=#008800]"arigato"[/color][color=#666600];[/color]

The selecting element will be hardcoded. I can adopt both approach selectbykey and selectbyname just it will be hardcoded and not from database.

Hi,

Following code do the same thing

If you want to select Japan. Here 3 is hardcoded

[color=#1C2837][size=2]<?php echo CHtml::dropDownList(‘country_id’,[/size][/color][size=2][color="#ff0000"]3[/color][/size][color=#1C2837][size=2], array(1=>‘USA’,2=>‘France’,3=>‘Japan’));?> … this is selecting Japan[/size][/color]

If i dont understand your requirement…please explain more…

I have taken a simple case like this but real case will be selecting from hardcoded value and then displaying corresponding model cgridview. For example if usa is selected then ‘dataprovider’ for cgridview will be Usa::model()->search() similarly France::model()->search() and Japan::model()->search().

I finally found the solution without hard coding selecting element.


echo CHtml::dropDownList('country_id','', array(1=>'USA',2=>'France',3=>'Japan'));


Yii::app()->clientScript->registerScript('update',"	 

$(document).ready(function(){     

  $('#country_id').change(function(){ 		

       switch ($(this).val()) { 			

         case '1':

           //condition 1; 				

           $.fn.yiiGridView.update(\"group-grid\"); 				

           break; 			

         case '2':

           // condition 2; 				

           $.fn.yiiGridView.update(\"group-grid\"); 				

           break; 			

         default:

           // condition 3;

           $.fn.yiiGridView.update(\"group-grid\"); 		

       }     

    }); 

});");



Javascript code can be modified to suit your needs.

I hope so this code will be helpful to others also.