Problem with dependent dropdown ( city , state )

Hi, I am using Yii dependent dropdown…

but problem is when I update date, default selected value not working for city… , for create it working fine.

This is my view code of state and city dropdown


<div class="row">

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

		<?php echo $form->dropDownList($model,'state',CHtml::listData(State::model()->findAll(), 'sid', 'sname'),

		array(

		'ajax' => array(

		'type'=>'POST', //request type

		'url'=>CController::createUrl('user/dynamicCities'), //url to call.

			//Style: CController::createUrl('currentController/methodToCall')

			'update'=>'#city', //selector to update

			array('class'=>'ajaxlink'),

			//'data'=>'js:javascript statement' 

			//leave out the data key to pass all form values through

		))); ?>

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

	</div>

	

	<div class="row">

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

		<?php	echo CHtml::dropDownList('city','', array($model->city)); ?>

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

	</div>

This is My controller code,


public function actionDynamicCities(){

		$data=City::model()->findAll('sid=:sid', 

                array(':sid'=>$_POST['User']['state']));

				

				$data=CHtml::listData($data,'cname','cname');

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

			    {

			       			   echo CHtml::tag('option',

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

			    }


	} 

and do anyone know how to put loading image with dropdown change, ( loading circle image )

thankx in advance

you need to make beforeSend function to show that image. after request finished you need to that element.

Edit.

check jquery ajax manual

Edit

this shows only loading image.

anyone else have more clear idea for both…

if you are making update you need to make this kind of stuff





<div class="row">

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

                <?php echo $form->dropDownList($model,'state',CHtml::listData(State::model()->findAll(), 'sid', 'sname'),

                array(

             	'id' => 'some_unique_id', // this is changes

                'ajax' => array(

                'type'=>'POST', //request type

                'url'=>CController::createUrl('user/dynamicCities'), //url to call.

                        //Style: CController::createUrl('currentController/methodToCall')

                        'update'=>'#city', //selector to update

                        array('class'=>'ajaxlink'),

                        //'data'=>'js:javascript statement' 

                        //leave out the data key to pass all form values through

                ))); ?>

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

.............

<?php


Yii::app()->clientScript->registerScript(__CLASS__,'

   $("#some_unique_id").trigger("change");

', CClientScript::POS_READY);



I face dthe problem with country,state,city dropdown .I applied ajax with the country dropdown but while applying on the state dropdown it doesn’t work?

Put your code in <div id=“myDiv”> and add ‘beforeSend’ and ‘complete’


'ajax' => array(

											'type' => 'POST',

											'beforeSend' => 'function(){$("#myDiv").addClass("loading");}',

											'complete' => 'function(){$("#myDiv").removeClass("loading");}',

											'dataType'=>'json',

											'url' => CController::createUrl('masterCountries/dynamicstates'),

CSS code


div.loading {

	background-color: #eee;

	background-image: url('loading.gif');

	background-position:  center center;

	background-repeat: no-repeat;

	opacity: 1;

}

div.loading * {

	opacity: .8;

} 

I have Used the ajaxbutton but it is not doing the ajax call to my action…

But if i use the same code with text field then it is doing the ajax call to the same action

can u please tell me how i can do ajax call through ajaxbutton

Thanks And Regards

Anand Neema

good discussion :lol: