Dependent Dropdown List

I’m having a problem getting AJAX to fire on a dropdown list

My form looks like:

<?php echo $form->dropDownList(

                    &#036;model,


                    'HomeState',


                    array(


                        CHtml::listData(HomeState::model()-&gt;findAll(),'StateCode','StateName'),


                        


                            'ajax' =&gt; array(


                                'type'=&gt;'POST', //request type


                                'url'=&gt;CController::createUrl('getCity'), //url to call.


                                'update'=&gt;'Address_CityCode', //selector to update


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





                                )


                        


                    ));

?>

Looking at firebug, when I change the value, what I see is the form Update then View, but my "getCity" never fires…

Any help would be greatly appreciated.

There’s at least something missing: you’re supposed to use a jQuery selector


…

'update'=>'#Address_CityCode', //notice the hash?

…

Thanks… one error fixed.

I’m still not seeing the getCity event being fired in Firebug.

I have the following in the CActiveForm:

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

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


    'enableClientValidation'=&gt;false,


'enableAjaxValidation'=&gt;true,

)); ?>

Is there something wrong there? I really do appreciate the assistance, I’m certainly no expert on jQuery or AJAX

Just a few typos:




<?php echo $form->dropDownList(

                        $model,

                        'HomeState',

                    	CHtml::listData(HomeState::model()->findAll(),'StateCode','StateName'),// no need to start array here, as CHtml::listData() returns array

                        	array(// start array here

                            	'ajax' => array(

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

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

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

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


                                    )

                        	)

                        ));

                        

  ?>




Should send request with these fixes.

And please check out all details for dropDownList function

Again… Thank you, very helpful. However… AJAX is still not firing. I know this same problem is posted on another forum, but I’m guessing I’m doing something wrong in setting up the AJAX.

In the actionUpdate function of the controller, I’ve uncommented the line:

<code>

$this->performAjaxValidation($model);

</code>

and in the _FORM which I’m using, I have:

<code>

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

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


    'enableClientValidation'=&gt;false,


'enableAjaxValidation'=&gt;true,

));

</code>

Am I subverting the AJAX calls somehow?

Thanks… I know us Newbies require a lot of patience.

Do you see any errors while in firebug "console" section?

Do you have any other js scripts working at this page?

I suppose jquery.js isn’t rendered at this page - you may see it directly in your html or while answering two previous questions.

Thanks for your response…

I’m getting one error on the Firebug console:

"NetworkError: 404 Not Found - http://hci/assets/b64b0cd3/jqueryslidemenu.css"

I doubt that’s related, but I’m a newbie…

I don’t believe there are other js on this page

I’d suggest to make these steps:

  1. Which way do you render jquery.js (with CClientSCript or with direct script call) ? Do you see it rendered?

  2. Please put test scripts on your page to make sure js scripts work at all


<script type="text/javascript">alert('hi');</script>

<script type="text/javascript">

	jQuery(document).ready(function() {alert('hi2');});

</script>



If problem still not found:

  1. Simplify problematic code, leaving only essential lines of code and provide it here (controller, view, layout) - everything I need to reproduce the feature, as the first example with provided fixes works just fine in my test environment.

Thank you so much!!! The jquery you supplied did indeed execute, but the drop down still is not… argghh!

OK, further clarification…

I placed a regular HTML drop down list on the form, which works fine:





        echo CHtml::dropDownList('country_id','', array(1=>'USA',2=>'France',3=>'Japan'),

            array(

                'ajax' => array(

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

                'url'=>CController::createUrl('CoverageLine/GetPractice'), //url to call.

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

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

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

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

            ))); 



The problem I’m having seems to be with CActiveForm. When I place the same logic within a CActiveForm element:




                echo $form->dropDownList(

                        $model,

                        'CommodityCode',

                        array(

                            CHtml::listData(Commodity::model()->findAll(),'CommodityCode','CommodityName'),

                            array(

                                'ajax' => array(

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

                                    'url'=>CController::createUrl('CoverageLine/GetPractice'), //url to call.

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

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

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

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

                                    )

                                )

                            )

                        ); 



In the CActiveForm version, it fires the form->Update instead of the form->GetPractice, and the GetPractice never gets fired…

Any ideas?

Same typo I described in the first post is in your second example:

array(//delete this array opening. also delete closing parenthesis for this array

                        CHtml::listData(Commodity::model()-&gt;findAll(),'CommodityCode','CommodityName'),

Check out parameters you need to pass in docs (I sent a reference before).

Hope it’ll help.

It works perfectly!!! Thank you so much for your assistance and patience with me!

Welcome :)

Have a nice coding.