Hi my fellow Yii members,
I have this problem for more than two days now. I cant replace these two field when the dropdown value is changed using ajax and json. Please help me where I went wrong
My view code is:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'activities-form',
'enableAjaxValidation'=>false,
)); ?>
<?php echo $form->errorSummary($model); ?>
<table cellpadding="0" cellspacing="0">
<tr><th>Objective</th>
<td><div class="row"><?php echo $form->dropDownList($model,'objid',$model->getObjectives(),
array(
'ajax'=> array(
'type'=>'POST', //request type
'dataType'=>'json',
'url'=>CController::createUrl('activities/getKT'), //url to call.
'success'=>'function(data){
$("#kpi").val(data.data1);
$("#targets").val(data.data2);
}')
));
?></div></td></tr>
<tr><th>KPI/Measures</th><td><span id="kpi"></span></td></tr>
<tr><th>Targets</th><td><span id="targets"></span></td></tr>
<tr><th>Initiatives</th>
<td><div class="simple">
<?php $this->widget('ext.widgets.xheditor.XHeditor',array(
'model'=>$model,
'modelAttribute'=>'actname',
'config'=>array(
'id'=>'xheditor_4',
'tools'=>'simple', // mini, simple, mfull, full or from XHeditor::$_tools, tool names are case sensitive
'skin'=>'o2007blue', // default, nostyle, o2007blue, o2007silver, vista
'width'=>'420px',
'height'=>'60px',
'loadCSS'=>XHtml::cssUrl('editor.css'),
),
)); ?>
</div></td></tr>
<tr><th>Weight(%)</th>
<td><div class="simple"><?php echo $form->textField($model,'weight',array('size'=>8,'maxlength'=>5)); ?></div></td></tr>
<tr><th> </th><th>
<?php echo CHtml::submitButton($model->isNewRecord ? 'Save' : 'Update'); ?>
</th></tr></table>
<?php $this->endWidget(); ?>
</div><!-- form -->
My controller code:
public function actiongetKT()
{
$objid = $_POST['Activities']['objid'];
$data = Yii::app()->db->createCommand("SELECT kpi,targets FROM objectives WHERE objid='$objid'")->queryRow(false);
echo CJSON::encode(array(
'data1'=>$data[0],
'data2'=>$data[1],
));
}