ajax dropdownlist problem

Hi,

I’m testing ajax features and I’m having a problem getting a dependant dropdownlist to work.

This is my form.php page :

<div class="form">

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

'id'=&gt;'batch-form',


'enableAjaxValidation'=&gt;false,

));

?>

&lt;p class=&quot;note&quot;&gt;Fields with &lt;span class=&quot;required&quot;&gt;*&lt;/span&gt; are required.&lt;/p&gt;





&lt;?php echo &#036;form-&gt;errorSummary(&#036;model); ?&gt;





&lt;div class=&quot;row&quot;&gt;


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


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'title',array('size'=&gt;60,'maxlength'=&gt;128)); ?&gt;


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


&lt;/div&gt;











&lt;div class=&quot;row&quot;&gt;


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


	&lt;?php 


	echo &#036;form-&gt;dropDownList(&#036;model,'batch_cop',


				array('0'=&gt;'None', '1'=&gt;'Class', '2'=&gt;'Package'), 


					array('ajax' =&gt; 


						array(


						   'type'=&gt;'POST',


								'url'=&gt;CController::createUrl('Batch/dynamicSubcategory'),


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


											)


										); ?&gt;


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


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


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


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


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


&lt;/div&gt;








&lt;div class=&quot;row buttons&quot;&gt;


	&lt;?php echo CHtml::submitButton(&#036;model-&gt;isNewRecord ? 'Create' : 'Save'); ?&gt;


&lt;/div&gt;

<?php $this->endWidget(); ?>

</div><!-- form -->

The BatchController class has the following methods:

public function actionDynamicSubcategory() {

		&#036;batch_cop = &#036;_POST['batch_cop'];





		if(&#036;batch_cop==1){


			&#036;data=Classes::model()-&gt;findAll();


			&#036;data=CHtml::listData(&#036;data,'id','title');


			


		}


		elseif(&#036;batch_cop==2){


			&#036;data=Package::model()-&gt;findAll();


			&#036;data=CHtml::listData(&#036;data,'id','title');


		}


		else{


			&#036;data=CHtml::listData(&#036;data,'id','title');


		}


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


            {


                echo CHtml::tag('option',


                           array('value'=&gt;&#036;value),CHtml::encode(&#036;title),true);


            }


    }

The problem is that when the change class/package dropdownlist nothing happens at "Class or Package" dropdownlist…plz anyone help me…

You canot update modelEx label couse model attribute is allredy called, and you dont updating this fild:

<?php echo $form->labelEx($model,‘Class or Package’); ?>

if you wont to do this you have to append <?php echo $form->labelEx($model,‘Class or Package’); ?> via ajax, i dont know other solution :)

Hi, this maybe help you

I also had this error, and I solved it by changing the following 2 things:

[list=1]

[*] When the you render a model based form, it creates the elements adding the name of the model (I do not know in depth about Yii, and do not know how to call this) and you must change the statement:

form this


'update'=>'cop_id') 

to this


'update'=>'#<name-of-model>_cop_id') //replace <name-of-model> with the name of the model used.

[*] when the ajax post the variables it doit as an array and cosiderate change the capture of the POST method like this

from this


$_POST['batch_cop']; 

to this


$_POST['<name-of-model>']['batch_cop']; //replace <name-of-model> with the name of the model used.

[/list]

I hope this help you to solve the problem

Adrian is right.

When you use $form->dropDownList, it generates the field called modelname_fieldname, but when you use CHtml->dropDownList, it generates the name you pass: fieldname.

The same rule applies to the $_POST:


$form->dropDownList >>> $_POST['modelname']['fieldname']

CHtml->dropDownList >>> $_POST['fieldname']

Hope that helps.

Thanks Adrian Arturo…

Now it is work very well…

thnx again.

Hehe.Thanks us very much.

This topic helped me.