Issue With Ajax Dropdown Box

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! :)

If your action isn’t being called, try adding your controller name to the first dropdowns’ ajax ‘url’ like this:




'url'=>CController::createUrl('myControllerName/myActionName'), // controller and action to call



@beninblack - Thanks for the reply,

but nope i tried that too, it still doesnt work.

I have defined my function in access rules as well. Cant understnad why this happens… :confused:

Thanks anyways…

Cheerz!

can someone help me please…

Reading your original post again:

I think you should check with a debug tool, like Google Chrome’s built in Dev tools to see if the ajax request is actually being sent. In Chromes Dev tools you click on Network >> XHR to see ajax requests(not sure about Firebug if you’re using that), and you can see lot’s of useful information about the request being made there.

Thanks a lot again for your reply,

Yup i did as you told and it gives me a 500 internal server error message

any ideas?

thanks a lot for your tip, i didnt know abt that debug tool :)

cheerz!

With the help of the debugger and some references i was able to have a dependent drop down list up and running.

Im going to spit it out hoping that another beginner will find it useful someday,

In my view




<div class="row">

            <?php 

            $current_classes=Schoolclasses::model()->findAll(array('select'=>'classId'));    

            echo $form->labelEx($model,'classId'); ?>

            

            <?php echo $form->dropDownList($model,'classId',CHtml::listData($current_classes,'classId','classId'),

                     array('empty'=>'-Select-',

                                      'ajax' => array(

                                        'type'=>'POST',

                                        'url'=>CController::createUrl('dynamicmake'),

                                        'update'=>'#Classtimetable_subjectName',

                                        'parent_id'=>'js:$(this).val()')

                           )

                    );    

            ?>

            

            <?php echo $form->error($model,'classId');?>

        </div>

        

        <div class="row">

            <?php echo $form->labelEx($model,'subjectName'); ?>

            <?php echo $form->dropDownList($model,'subjectName',array('empty'=>'-Select-')); ?>

            <?php echo $form->error($model,'subjectName');?>

        </div>



where subjectName depends on the value of the classId selected.

In my controller




public function actionDynamicmake()

    {

    

    $clsID = $_POST['Classtimetable']['classId']; // IMPORTANT .. this is how you access previously entered data

    

    $records1 = Subjects::model()->findAll('taughtinClass=:id',

            array(

            ':id'=>$clsID ,

             )); 

    

            $data1 = CHtml::listData($records1,'subjectName','subjectName');

                

            foreach($data1 as $value=>$name)

            {

            echo CHtml::tag('option',

                   array('value'=>$value),CHtml::encode($name),true);

            }

    }



[b]dont forget to add the function (i.e. dynamicmake - in my case) to access rules

[/b]

Hope someone’ll find this useful

Cheerz!