Hi friends,
I’m working with the drop down list and working fine while creation and deletion, but my problem is during the updating of that particular form…
I’m having pname(program name ) and cname(course name), when I’m clicking updating button the pname field loads automatically, but cname is not set and while submitting the form that field shows error and I need to re choose the pname field and cname field for every other field updating also it really create irritation for updating…
If any suggestion to keep that drop down field always set based on database values…
This is my code in view .
<?php echo $form->dropDownList($model,'pname',CHtml::listData(Programs::model()->findAll(
array('order' => 'pname')),'pname', 'pname'),
array(
'class'=>'form-control',
'prompt'=>'Choose your course',
'ajax'=>array(
'type'=>'POST',
'url' => CController::createUrl('findcourses'),
'data'=> array('pname'=>'js:this.value'),
'update'=>'#'.CHtml::activeId($model,'cname'),))
);
?>
<?php echo $form->error($model,'pname'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'cname'); ?>
<?php echo $form->dropDownList($model,'cname', array(),
array('prompt'=>'Choose stream',
'class'=>'form-control',
'ajax'=>array(
'type'=>'POST',
'url' => CController::createUrl('findfees'),
'data'=> array('cname'=>'js:this.value'),
'update'=>'#cfees',)
)); ?>
<?php echo $form->error($model,'cname'); ?>
</div>
in controller
public function actionFindcourses()
{
//$model=new Students;
//$model->cfees=200;
$model=new Students;
//$model->totalcharges=500;
$date=Yii::app()->dateFormatter->formatDateTime(time(), 'medium', false);
$model->date= $date;
$data = Courses::model()->findAll('pname=:pname',
array(':pname'=>$_POST['pname']));
$data=CHtml::listData($data,'cname','cname');
echo "<option value=''>Courses</option>";
foreach($data as $value=>$name)
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);
}