Hi,
This problem is probably very easy for most of you but it took lots of time for me and couldn’t solve it yet. I have two dropdown lists which are campus,building.Building is dependent on campus.I used ajax to solve dependencies.In my location table, there are three columns which are id,campus,building. I use these dropdown lists to add an “event”. Event table holds only the location id which corresponds to id in location table. When I select campus and building on dropdown lists in create form of “event” and click on create button, it says “Please fix the following input errors: campus cannot be blank” and I lost the inputs of campus and building after refresh.
"_form" view:
<div class="row">
<?php echo $form->labelEx($model,'locationId'); ?>
<?php echo CHtml::dropDownList('campus','', CHtml::listData(Location::model()->findAll(), 'campus', 'campus'),
array(
'empty' =>'--please select--',
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('dynamicBuildings'), //url to call.
//Style: CController::createUrl('currentController/methodToCall')
'update'=>'#building', //selector to update
//'data'=>'js:javascript statement'
//leave out the data key to pass all form values through
)));
?>
<?php echo $form->error(Location::model(),'campus'); ?>
</div>
<div class="row">
<?php echo $form->labelEx(Location::model(), 'building'); ?>
<?php echo CHtml::dropDownList('building','', array(), array('empty' =>'--please select campus first--')); ?>
<?php echo $form->error(Location::model(),'building'); ?>
</div>
EventController:
public function actionDynamicBuildings()
{
//please enter current controller name because yii send multi dim array
$data=Location::model()->findAll('campus=:campus',
array(':campus'=> $_POST['campus']));
$data=CHtml::listData($data,'id','building');
if($data == null){
echo CHtml::tag('option',
array('empty'),CHtml::encode('--please select campus first--'),true);
}
else{
echo CHtml::tag('option',
array('empty'),CHtml::encode('--please select--'),true);
}
foreach($data as $value=>$building)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($building),true);
}
}
What can be the problem?
Thanks