Dependent Dropdown

Hi,

I'm having some troubles with the dependent dropdownlist, I found some code in this site so I adapted my code (you can see it below), once the first dropdownlist is selected, the second doesn't update...can anybody help me?, please.





Thanks... =)

_form.php

<div class="row">

&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'estado_id'); ?&gt;


&lt;?php echo &#036;form-&gt;dropDownList(&#036;model,'estado_id',CHTML::listData(ebeca_estados::model()-&gt;findAll(),


	'estado_id','estado'), 


	array('empty'=&gt;'--please select--'), 


	array('ajax'=&gt;array('type'=&gt;'POST',


	                    'data'=&gt;array('estado_id'=&gt;'js.this.value'),


		  	    'url'=&gt;CController::createURL('ebeca_municipios/getmunicipios'),


			    'update'=&gt;'#estado_id',))); ?&gt;


&lt;?php echo &#036;form-&gt;error(&#036;model,'estado'); ?&gt;

</div>

<div class="row">

&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'municipio_id'); ?&gt;


&lt;?php echo &#036;form-&gt;dropDownList(&#036;model,'municipio_id',array()); ?&gt;


&lt;?php echo &#036;form-&gt;error(&#036;model,'municipio_id'); ?&gt;

</div>

_Ebeca_municipiosController

public function actionGetmunicipios()

{

&#036;data=ebeca_municipios::model()-&gt;findAll('estado_id:=estado_id',


array(':estado_id'=&gt;&#036;_POST[estado_id]));


&#036;data=CHTML::listData(&#036;data,'municipio_id','municipio');





foreach (&#036;data as &#036;value =&gt; &#036;name) {


	echo CHTML::tag('option', 


	array('value'=&gt;&#036;name),CHTML::enconde(&#036;name),true);

}

There are indeed many tutorials around on this site. It is one of those simple things that until you understand it, can appear like its not working.

Here are some pointers;

[list=1][]if you have filters() and accessRules() defined in your controller make sure you also have ‘ajaxOnly + getmunicipios’ and ‘allow’=>‘getmunicipios’ set respectively.[]Check in firebug or chrome developer tool to see what response you are getting[]also remember that code in public function actionGetmunicipios() {…} will only work when actual ajax request is made.[]Because you are using cactiveform the line


'update'=>'#estado_id',))); ?>

should be something like


 'update'=>'#'.CHtml::activeId($model, 'municipio_id',))); ?>

// NOTE: I put ‘municipio_id’ rather than estado as I assume that is your target to update field[/list]

If you follow this wiki 24…I can assure you that it will work you.

If not, then I recommend you post the errors you are getting from both browser tool or app response. This way others may be able to shine the light to areas you need to pay attention

I only ask that you post your working solution back to the community once you have it working

Hi,

Thanks a lot for your answer, I read the link that you send me, and I realized that it was a lot of errors in the code that I post, buy finally it works, here is the code with all the corrections.





 Thanks and have a nice day =)

__form.php

      echo &#036;form-&gt;labelEx(&#036;model,'estado_id'); 


	 	echo &#036;form-&gt;dropDownList(&#036;model,'estado_id',CHtml::listData(ebeca_estados::model()-&gt;findAll(),


		'estado_id','estado'), 


					array(


						'empty'=&gt;'--please select--',


						'ajax'=&gt;array('type'=&gt;'POST',


								'data'=&gt;array('estado_id'=&gt;'js:this.value'),


								'url'=&gt;CController::createURL('ebeca_municipios/getmunicipios'),


								'update'=&gt;'#municipio_id'))); 


		echo &#036;form-&gt;error(&#036;model,'estado_id'); 





		echo &#036;form-&gt;labelEx(&#036;model,'municipio_id');


		echo CHtml::dropDownList('municipio_id','',array(),array('prompt'=&gt;'PleaseSelect'));


		echo &#036;form-&gt;error(&#036;model,'municipio_id'); 

__ebeca_municipiosController

 public function actionGetmunicipios()


 {


	&#036;data=ebeca_municipios::model()-&gt;findAll('estado_id=:estado_id', array(


								':estado_id'=&gt;&#036;_POST['estado_id']));


																					


	&#036;data=CHtml::listData(&#036;data,'municipio_id','municipio');


	foreach (&#036;data as &#036;value=&gt;&#036;name) {


		echo CHtml::tag('option',array('value'=&gt;&#036;value),CHtml::encode(&#036;name),true);


	


	}						


 }

Good on you friend

hi seal;

i am using the dependent drop down list it’s working correctly during creation…

during the update process… first drop down list showing values . in second drop down list nothing available…

Thanks.

How about posting your controller and relevant views?

Dude. I just tried something like this…

Sorry. I’m on the rush and can’t comment the code. Check if you can understand by yourself.




<?php echo $form->textFieldRow($model, 'zipcode', array('class'=>'span5', 'maxlength'=>10)); ?>


<?php echo $form->textFieldRow($model, 'address', array('class'=>'span5', 'maxlength'=>255)); ?>


<?php echo $form->textFieldRow($model, 'number', array('class'=>'span5', 'maxlength'=>10)); ?>


<?php echo $form->textFieldRow($model, 'complement', array('class'=>'span5', 'maxlength'=>150)); ?>


<?php echo $form->textFieldRow($model, 'neighborhood', array('class'=>'span5', 'maxlength'=>100)); ?>


<?php if($model->isNewRecord): ?>


	<?php echo $form->dropDownListRow($model, 'country', CHtml::listData(Countries::model()->findAll(array('order'=>'name ASC')), 'id', 'name'), array('class'=>'span5', 'prompt'=>Yii::t('interface', 'Select one country'), 

		'ajax'=>array(

			'type'=>'GET',

			'url'=>$this->createUrl('suggestStates'), 

			'data'=>array('cid'=>'js:$(this).val()'), 

			'update'=>'#Properties_state',

			'afterAjaxUpdate'=>'js:$("#Properties_state").attr("disabled", false)', 

		))); ?>

			

	<?php echo $form->dropDownListRow($model, 'state', array(), array('class'=>'span5', 'disabled'=>'true', 'prompt'=>Yii::t('interface', 'Select one state'), 

		'ajax'=>array(

			'type'=>'GET', 

			'url'=>$this->createUrl('suggestCities'), 

			'data'=>array('cid'=>'js:$("#Properties_country").val()', 'sid'=>'js:$(this).val()'), 

			'update'=>'#Properties_city', 

			'afterAjaxUpdate'=>'js:$("#Properties_city").attr("disabled", false)', 

		))); ?>		

		

	<?php echo $form->dropDownListRow($model, 'city', array(), array('class'=>'span5', 'disabled'=>'true', 'prompt'=>Yii::t('interface', 'Select one city'))); ?>


<?php else: ?>


	<?php echo $form->dropDownListRow($model, 'country', CHtml::listData(Countries::model()->findAll(array('order'=>'name ASC')), 'id', 'name'), array('class'=>'span5', 'prompt'=>Yii::t('interface', 'Select one country'), 

		'ajax'=>array(

			'type'=>'GET',

			'url'=>$this->createUrl('suggestStates'), 

			'data'=>array('cid'=>'js:$(this).val()'), 

			'update'=>'#Properties_state',

			'afterAjaxUpdate'=>'js:$("#Properties_state").attr("disabled", false)', 

		))); ?>

		

	<?php echo $form->hiddenField($model, 'state', array('id'=>'state', 'name'=>'state')); ?>

			

	<?php echo $form->dropDownListRow($model, 'state', array(), array('class'=>'span5', 'disabled'=>'true', 'prompt'=>Yii::t('interface', 'Select one state'), 

		'ajax'=>array(

			'type'=>'GET', 

			'url'=>$this->createUrl('suggestCities'), 

			'data'=>array('cid'=>'js:$("#Properties_country").val()', 'sid'=>'js:$(this).val()'), 

			'update'=>'#Properties_city', 

			'afterAjaxUpdate'=>'js:$("#Properties_city").attr("disabled", false)', 

		))); ?>

		

	<?php echo $form->hiddenField($model, 'city', array('id'=>'city', 'name'=>'city')); ?>

		

	<?php echo $form->dropDownListRow($model, 'city', array(), array('class'=>'span5', 'disabled'=>'true', 'prompt'=>Yii::t('interface', 'Select one city'))); ?>

	

	<?php Yii::app()->clientScript->registerScript(uniqid(), "

		$(window).load(function(){

			$.ajax({

				type		:	'GET',

				url		:	'" . $this->createUrl('suggestStates') . "', 

				data		: 	{ cid: $('#Properties_country').val(), },

				success	:	function (data) { 

					$('#Properties_state').html(data);

					$('#Properties_state').val($('#state').val())

					$.ajax({

									type		:	'GET',

									url		:	'" . $this->createUrl('suggestCities') . "', 

									data		: 	{ cid: $('#Properties_country').val(), 'sid': $('#Properties_state').val() },

									success	:	function (data) { 

										$('#Properties_city').html(data);

										$('#Properties_city').val($('#city').val())

					},

					});

				},

				});

			$('#Properties_state').attr('disabled', false);

			$('#Properties_city').attr('disabled', false);

		});

	");

	?> 

	

<?php endif; ?>