Hello guys, hope you are well!
Im quite new to YII and is stuck with the AJAX part. Ive been trying to do this for two days, with no success at all.
What i want to do is fairly simple, I have one drop down list which shows the class Id’s, once I select the class Id i want another drop down list to be populated with the subjects that that particular class teaches.
In my view
<div class="row">
<?php echo CHtml::beginForm();?>
<?php $current_classes=Schoolclasses::model()->findAll(array('select'=>'classId'))?>
<?php echo $form->labelEx($model,'classId'); ?>
<?php echo CHtml::dropDownList('myDropDown','',CHtml::listData($current_classes,'classId','classId'),array(
'ajax' =>
array(
'type'=>'POST', //request type
'url'=>CController::createUrl('myActionName'), //action to call
'update'=>'#myDropDown1', // which HTML element to update
)
)); ?>
<?php echo CHtml::dropDownList('myDropDown1','',array()); ?>
<?php echo $form->error($model,'classId');
echo CHtml::endForm();
?>
</div>
where myDropDown is the dropdownlist containing the class Id’s and myDropDown1 is the one to be updated.
In my controller i defined the myActionName
public function actionMyActionName()
{
print_r("this is");
$cls_ID = $_POST['myDropDown']; // IMPORTANT .. this is how you access previously entered data
$records1 = Subjects::model()->findAll('taughtinClass=:id',
array(
':id'=>$cls_ID ,
));
$data1 = CHtml::listData($records1,'subjectName');
foreach($data1 as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
I tried having a print_r in my action to see if it get executed but it wasnt printed.
Please help me if you can…!
Thanks in advance!
Cheerz!