1st : go to view/_form.php where you want your Drop down .
[b]
2nd : [/b]copy past the following code with your won model names
// Our Drop Down
<div class="row">
<?php echo $form->labelEx($model,'product_name'); ?>
<?php
$products = CHtml::listData(product::model()->findAll(),'product_id','product_name');
$options = array(
'empty' => '--Select a Product--',
'ajax' => array('type'=>'POST'
, 'url'=>CController::createUrl('SalesReport/GetPrice')
, 'data'=> array('product_name'=>'js:this.value')
, 'update'=>'#price_id'
//selector to update
)
);
//echo $form->field($model, 'product_name')->dropDownList('product_name', '', $products, $options);
//echo CHtml::dropDownList('product_name', '', $products, $options);
echo $form->dropDownList($model,'product_name',CHtml::listData(product::model()->findAll(),'product_id','product_name'), $options,array('onchange'=>'get_amount(this);'));
?>
<?php echo $form->error($model,'product_name'); ?>
</div>
// Our text field
<div class="row" >
<?php echo $form->labelEx($model,'product_sale_rate'); ?>
<div id="price_id" >
<?php echo $form->textField($model,'product_sale_rate',array('id'=>'sale_rate','onchange'=>'get_amount(this);')); ?>
</div>
<?php echo $form->error($model,'product_sale_rate'); ?>
</div>
3rd : Now go to your controller Copy past the code with your won query
public function actionGetPrice()
{
//Model::model()->price();
$product_id = (int)$_POST['product_name'];
//echo $product_id;
$data = Yii::app()->db->createCommand()->
select('product_sale_rate')->
from('product')->
where(array('like', 'product_id',$product_id))->
queryAll();
//var_dump($data);
$a= array_shift($data);
$result= array_shift($a);
$model=salesreport::model();
echo CHtml::activetextField($model,'product_sale_rate', array('value' => $result,'onkeypress'=>'get_amount(this);','id'=>'sale_rate'));
}
4th : Update your accessRules in controller
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete','GetPrice'),
'users'=>array('admin'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
Ok . You Done .