Setting Dependent Dropdown Values During Update

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.

Hi Apaul,

if i understand correctly your problem ajax code is used only when user click on dropdownlist, so the values are not set when entering the form for update. You must set values by adding php code before displaying the dropdownlists if the record is existing.

Hope it will help.

OK. Good Morning . :rolleyes:

1st . Add a PHP code that shows the current value of the text field from databases.

2nd . Display the Drop down menu when some one click on the Text field .

Try Your self . Take help from Google … Other wise I will try to help you In another way .

Thanks ragua and Debasis,

Let me try them out and get back.

Thanks guys for the suggestions.

I managed to fix the problem by substituting the empty array with an array of database values.It’s now working exactly the way I wanted.

Cheers!