Hi friends,
My code for dependent drop down is working well for create action but does not show the current database value for update action.Only the first drop down shows the current database value whereas the dependent drop down shows the prompt text only.
My code is as below
View
<div class="row">
<?php echo $form->labelEx($model,'countyid'); ?>
<?php
$records= CHtml::listData(Counties::model()->findAll(),'countyid','countyname');
echo $form->dropDownList($model,'countyid',$records,array(
'prompt'=>'Select County','style'=>'width:210px',
'ajax'=>array(
'type'=>'POST',
'url'=> CController::createUrl('loadconstituencies'),
'update'=>'#'.CHtml::activeId($model,'constituencyid'),
)
));
?>
<?php echo $form->error($model,'countyid'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'constituencyid'); ?>
<?php
echo $form->dropDownList($model,'constituencyid',array(),array(
'prompt'=>'Select Constituency','style'=>'width:210px',
'ajax'=>array(
'type'=>'POST',
'url'=>CController::createUrl('loadwards'),
'update'=>'#'.CHtml::activeId($model,'ward_id'),
)
));
?>
<?php echo $form->error($model,'constituencyid'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'ward_id'); ?>
<?php echo $form->dropDownList($model,'ward_id',array(),array('prompt'=>'Select Ward','style'=>'width:210px')); ?>
<?php echo $form->error($model,'ward_id'); ?>
</div>
Controller
public function actionLoadconstituencies()
{
$countyid = $_POST['Businessdetails']['countyid'];
$data= Constituencies::model()->findAll('countyid=:countyid',array(':countyid'=> $countyid));
$data=CHtml::listData($data,'constituencyid','constituencyname');
echo CHtml::tag('option', array('value'=>''),CHtml::encode('Select Constituency'),true);
foreach($data as $value=>$name)
{
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);
}
}
public function actionLoadwards()
{
$constituencyid = $_POST['Businessdetails']['constituencyid'];
$data= Wards::model()->findAll('constituencyid=:constituencyid',array(':constituencyid'=> $constituencyid));
$data=CHtml::listData($data,'ward_id','wardname');
echo CHtml::tag('option', array('value'=>''),CHtml::encode('Select Ward'),true);
foreach($data as $value=>$name)
{
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);
}
}
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if(isset($_POST['Businessdetails']))
{
$model->attributes=$_POST['Businessdetails'];
if($model->save())
{
$this->redirect(array('bizeditconfirmation'));
}
}
$this->render('editbizdetails',array('model'=>$model,
));
}
Kindly advise on how I can show current database values for dependent drop down during update action.