Dropdownlist Selection To Autofill A Textbox

Hi guyz,

Been trying to achieve this but it has proven hard to my knowledge. I have a dropdownlist that uses values from a database table. The table used by the dropdownlist is called services and has only 2columns named name-which holds the services name and amount-which holds the service price. Its is currently populated with this data




id----------name-----------amount

1----------braiding---------1500

2----------plaiting-----------2000

3----------weaving----------1500

4-----------flatiron-----------500



This works very fine. The model name is called Services.The code that displays the dropdownlist is below




<?php echo $form->dropDownList($model,'name', CHtml::listData(Services::model()->findAll(array('order' => 'name ASC')), "amount", "name"), array('empty'=>'select service')); ?>



So the dropdownlist is populated with names of the services.

I have another Table named client_services and as got several columns. Among them is client_name, amount and name among others. Its model is called ClientServices and its _form.php houses the above dropdownlist. See the code below




.......

......

.....

<div class="row">

		<?php echo $form->labelEx($model,'name'); ?>

		<?php echo $form->dropDownList($model,'name', CHtml::listData(Services::model()->findAll(array('order' => 'name ASC')), "amount", "name"), array('empty'=>'select service')); ?>

		

		<?php echo $form->error($model,'name'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'datetime'); ?>

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

		<?php echo $form->error($model,'datetime'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'amount'); ?>

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

		<?php echo $form->error($model,'amount'); ?>

		

	</div>

...........

...........

..........



so what am trying to achieve is I want when I select a service, for exapmle Braiding, the amount textfield should be automatically populated with 1500, if i change the selection to plaiting, the textbox should be populated wth 2000.

I tried to achieve its with the below script but without success




<script>

Yii::app()->clientScript->registerScript(uniqid(), "

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

	$('#ClientService_amount').val($('#name_amount_val option:selected').html());

	$('#ClientService_amount').val($(this).val());

});

");

</script>



Any help would be highly appreciated

Thanks in advance.

Hi,

please see this link…

http://www.yiiframework.com/forum/index.php/topic/45990-dynamic-loading-data-in-selection-box/

you want to create the CHtml::textfield and update the textfield on drop-down ajax…