Dependent Drop Down List

Hi,

I started training myself Yii framework three weeks ago.

I kindly need help on dependent drop down list.I have read many wikis but my problem still persists.

I have three tables namely;

Businessdetails

pinnumber PK

countyid

constituencyid

ward

Counties

countyid PK

countyname

Constituencies

countyid

constituencyid PK

constiyuencyname

I have a form that allows one to register business information to businessdetails table. The form is already loading counties from the table ‘counties’ as a dropdown. I want when one selects a county, constituencies under that county are loaded in the constituency as a dropdown.

My view and controller code are below but not working.

[size="5"]VIEW[/size]




<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'businessdetails-form',

	// Please note: When you enable ajax validation, make sure the corresponding

	// controller action is handling ajax validation correctly.

	// There is a call to performAjaxValidation() commented in generated controller code.

	// See class documentation of CActiveForm for details on this.

	'enableAjaxValidation'=>true,

        'clientOptions'=>array('validateOnSubmit'=>true),

)); ?>

<div class="row">

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

		<?php 

echo $form->dropDownList($model,'countyid',CHtml::listData(Counties::model()->findAll(), 'countyid', 'countyname'),

                    array(

                        'prompt'=>'-Select County-',

                        'ajax' => array

                        (

                        'type'=>'POST', 

                        'url'=>CController::createUrl('Businessdetails/loadconstituencies'), //or $this->createUrl('loadcities') if '$this' extends CController

                        'update'=>'#constituencyid', //or 'success' => 'function(data){...handle the data in the way you want...}',

                        'data'=>array('countyid'=>'js:this.value'),

                        )));

                ?>

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

	</div>

	<div class="row">

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

		<?php echo $form->dropDownList($model,'constituencyid',array()); ?>

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

	</div>




[size="5"]CONTROLLER[/size]





public function actionLoadconstituencies()

{

$data=  Constituencies::model()->findAll('countyid=:countyid',array(':countyid'=>(int) $_POST['countyid']));


                $data=CHtml::listData($data,'constituencyid','constituencyname');


            foreach($data as $value=>$constituency)

            {

                echo CHtml::tag('option', array('value'=>$value),CHtml::encode($constituency),true);

            }


        }






Kindly help

may be this will help you, try these links -

http://www.yiiframework.com/wiki/730/how-to-create-the-custom-drop-down-using-chtml-dropdownlist/

http://www.yiiframework.com/wiki/723/creating-a-dependent-dropdown-from-scratch-in-yii2/

I think this will help you

Hi guys,

Thanks for suggesting solutions to my problem.

I have tried each implementation and now Counties are showing in the drop down but when selected,respective constituencies do not load.

I have a feeling that Ajax is not calling the controller action.Even when I write a controller action name that doesn’t exist in the url(‘url’=> CController::createUrl(‘Businessdetails/loadconstituencies’)),I neither get an error nor see a change in constituencies drop down.

Kindly help me know what is missing to get Ajax working.

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-',

                   	'ajax'=>array(

                   	'type'=>'POST',

                   	'url'=> CController::createUrl('Businessdetails/loadconstituencies'),

                   	'update'=>'#'.CHtml::activeId($model,'constituencyid')

                   	'data'=>array('countyid' => 'js:this.value'),

                       	

                   	)

                   	));

   	

                ?>

		<?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-')); ?>

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

	</div>



CONTROLLER




public function actionLoadconstituencies()

        {

            $countyid = $_POST['Businessdetails']['countyid'];

            $data=Constituencies::model()->findAll('countyid=:countyid',array(':countyid'=> $countyid));


            $data=CHtml::listData($data,'constituencyid','constituencyname');

            

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

            {

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

            }

        }



Hi apaul

First thing to do is to check if ajax is working and if yes what values are returned by the ajax call. Use firebug for firefox or developer tools for chrome to see the actual ajax call. There you can see if the call has been made… if it was successful… and what is the returned value of the call.

Thanks Maurizio and everyone for your help.

I have inspected Ajax with Chrome and realized the error.I had not added the action ‘loadconstituencies’ to the access rules.

It is now working perfectly.

Follow the link if you have any other issue related to this …