Get Both Id And Description From Single-Line Dropdownlist


I have a ‘single line’ dropdownlist wich obtains its $data (id and description) from my db.

The dropdownlist is ‘independent’ and thus does not store the selected id in a model.

I use ajax to send the selected id (obtained with ‘js:this.value’) to my controller.

My question is:

How can I also send the description to my controller.

Currently, in the controller, I have to use the received id to read the database again - just to get the description. But it should be possible to send both the id and description to the controller at the same time.

You could do something like:


  echo $form->dropDownList($model, 'id', CHtml::listData(<ModelNameContainingDesriptions>::model()->findAll(), 'description', 'description'));


This way, the value of the option will be the description instead of the ID.



Hi SilverPT, thanx for the reply.

I think we misunderstood each other. My problem is not getting data for the dropdownlist, but rather to get the selected data out - ALL of it, not just the id.

This is my dropdownlist:


	$addListData = CHtml::listData(ar_trn5::model()->findAll(),

		'trn5_id', 'trn5_desc');


	echo CHtml::dropDownList('myDrop1', '', $addListData,


			'class'		=>	'singleline',

			'size'		=>	'1',

			'prompt'	=>	'Add a new Level',

			'ajax'	=> array(

				'type'	=>	'GET',

				'url'	=> 	$this->createUrl('addtochildarray'),

				'data'	=>	array(

					'idValue'	=> 'js:this.value',

					'descValue'	=> <img src='' class='bbc_emoticon' alt='???' />?<img src='' class='bbc_emoticon' alt='???' />??,

				'success'=>	'function(data){







I store the value of trn5_id in idValue by using ‘js:this.value’

Now I need to get the value of trn5_desc and store it in descValue.


I have solution but its not a perfect solution. Don’t use CHtml::listData… Create your own array. You make an array whose index contains id and description combination. For example id=1 and description=‘test’ then make index like 1,test. So using ajax request you get 1,test in your controller then you can perform string operation like explode. Hope its working.

Hi Ravi,

Yes, that sounds like a good idea if we can’t use js to extract the description. But it surely is a lot of extra hassle.

So I hope we can get it done with js.


Ravi’s suggestion seems good.

Apart from that the other way I see is to create a hidden field, and when the user selects an option from the dropdown list, you can create a JS that has “onChange” event that puts the value of the ID of the option selected concatenated with the description into a hidden field’s value.

Or 2 hidden fields, one with the ID, other with the Description, so you don’t have to do explodes in the controller and could access the parameters like


<?php echo $form->hiddenField($model,'selected_id'); ?>

<?php echo $form->hiddenField($model,'selected_description'); ?>


$selected_id = $_POST['model']['selected_id']

$selected_description = $_POST['model']['selected_description ']

Either way, this requires more validation from the controller the assure that the user did not mess with the values.

Apart from that, this is still based on Ravi’s solution.

But maybe mine is overdone and not worth the effort, your call :)



Yo SilverPT

You wrote: “you can create a JS that has “onChange” event that puts the value of the ID of the option selected concatenated with the description into a hidden field’s value.”

This is my whole problem. How does that JS function look like (getting both the id and description)?

Hum I believe it would be something like:

(be sure to include JQuery library)

<script type="text/javascript">


       $("#<id_of_dropdown>").change(function() {

           var seleted_id = $(this).val();

           var selected_description = $(this).text();







Then you can catch it with through $_POST, and since it is not related to any model, it would probably be just $_POST[‘name of hidden field’].

But like I said, be sure to validate the input of the controller as the user might hack it by inserting non-matching ID’s with Descriptions in the DOM.



Ya SilverPT, I quickly tried ‘js:this.text’ and it did not work. But maybe, as you said, it is more secure to just pass the id and get the description again from the db. So I think I will go with that.

But thanx for the input anyway.