dependent dropdown

hi all,

   i am new to yii.I have created 3 tables called society,block and flats.if i select society i should get respective blocks populated in block dropdown.my _form.php code

   <div class="row">

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

        <?php

              $block = new CDbCriteria;

              $block->order = 'block_name ASC';

        ?>

        <?php

              echo $form->dropDownList($model,'block_name',CHtml::listData(block::model()->findAll($block),'id','block_name'),

                        array(

                            'ajax' => array(

                            'type' => 'POST',

                            'url' => CController::createUrl('Apartment/Dynamicblocks'),

                            'update' => '#block_name',

                        )

                  )

              );

        ?>

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

    </div>

my controller code:


public function actionDynamicblocks()

    {

        $data = apartment::model()->findAll('id=:sname',

                        array(':sname'=> $_POST['apartment']['sname']));




        $data = CHtml::listData($data,'id','block_name');

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

            {

                echo CHtml::tag('option',array('value' => $id),CHtml::encode($value),true);

            }


    }



i have place both these codes in flat controller and form files.

when i click on society dropdown and select a society i am not getting anything in the other dropdown.

please help me

thanks

ramakrishna

First of all I would suggest you to read this wiki - http://www.yiiframew…endent-dropdown

and to try to make working the example from the article… so that you get a bit of experience and that you get to know how this all works…

And there are many "dependent dropdown" threads in the forum try a bit to search for them…

As for your code… the first problem is that the first dropdown "block_name" is updating himself instead the second dropdown…

Than for your response.I have tried that but there is no effect of that function which i defined in controller when i call it from view.please help me

What have you tried:

did you read the article?

did you make the article example working?

did you search a bit the forum for other dependent dropdown examples?

hi all,

i have tried the wiki example of dependent dropdown lists.I have not succeeded in populating the other dropdown when i select other dropdown.here is my code

_form.php


<div class="row">

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

        <?php

              $country = new CDbCriteria;

              $country->order = 'name ASC';

        ?>

        <?php

              echo $form->dropDownList($model,'id',CHtml::listData(wsmembersdetails::model()->findAll($country),'id','name'),

                        array(

                            'ajax' => array(

                            'type' => 'POST',

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

                            'update' => '#worldareascities_'.stateid

                        )

                  )

              );

        ?>

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

    </div>



controllercode:




 public function actionDynamicstates()

    {

        $data = Worldareasstates::model()->findAll('countryid=:id',

                        array(':id'=>(int) $_POST['Wsmembersdetails']['countryid']));




        $data = CHtml::listData($data,'id','name');

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

            {

                echo CHtml::tag('option',array('value' => $id),CHtml::encode($value),true);

            }


    }



   I have created 3 tables called

wsmembersdetails

worldareastates

worldareascities

i have placed the above codes in worldareascities.when i run it, it says “Use of undefined constant stateid - assumed ‘stateid’” .

the problem is in the line


'update' => '#worldareascities_'.stateid

what is stateid?

If it’s a variable then you should use $stateid !

NOTE: I merged your two posts… please do not open new threads for the same problem…

thank for reply.

stateid is not a variable its a column in the worldareascities table.

in wiki he has mentioned wsmembersdetails what is it?is it a table in database?

Please STOP posting duplicate posts… there is really no need to open a new thread for every problem you get on dependent dropdown !

wsmembersdetails is used in the comments… that’s code from the user sasor… wsmembersdetails is his model… but his comment is about triple dropdown…

Try to make the example from the article… country -> city… as simple as it can be… and only when you understand how it works… you can make it complex for 3 dropdowns…

hi,

i have tried the dependent dropdown for countries and states wherein if i select a country i should get respective states

_form.php code:




<div class="row">

	<?php


              echo $form->dropDownList($model,'id',CHtml::listData(wsmembersdetails::model()->findAll(),'id','name'),array('prompt'=>'--Select--'),

                        array(

                            'ajax' => array(

                            'type' => 'POST',

                            'url' => CController::createUrl('worldareastates/Dynamicstates'),

                            'update' => '#'.'id',

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

                        )

                  )

              );

        ?>


                <?php

                 echo $form->dropDownList($model,'id', CHtml::listData(worldareastates::model()->findAll(),'id','name'));?>


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

    </div>



controller code:


 public function actionDynamicstates()

    {

        $data = Worldareasstates::model()->findAll('countryid=:id',

                        array(':id'=>(int) $_POST['Wsmembersdetails']['countryid']));




        $data = CHtml::listData($data,'id','name');

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

            {

                echo CHtml::tag('option',array('value' => $_POST[wsmembersdetails]),CHtml::encode($value),true);

            }


    }



when i select country i dont see anything in other dropdown.should i add any extra code for controller or should i modify any syntax.should i add any onchange events when i select firstdropdown so that changes occur in second dropdown.

please help me

thanks

ramakrishna

You are missing some basic knowledge here…

First dropdown is


$form->dropDownList($model,'id',...

and you put the same for the second one… you need a unique ID for every input field… so the second dropdown should be different… .something like


$form->dropDownList($model,'city',...

when you render the page… check your HTML source… check what you got there… what is the ID of the second dropdown… that ID you need to put in the


'update'=>'#'.<the ID of the second dropdown'

In the actionDynamicstates you are setting all values the same - $_POST[wsmembersdetails] - that is wrong

how onchange event occurs with the above code when i select a value in one dropdown.I have two tables wsmembersdetails and worldareastates and i kept this code in worldareastates.when i run application there is no effect.No errors are displayed.please help me.

Hi nalajala,

I can’t help but notice, but I don’t think you have followed the wiki very well. Why do you keep on using ‘wsmembersdetails’? It is not from the wiki, it is from the user comments. Follow the wiki step by step. I have done it, so there’s nothing wrong about the wiki.

Here are some pointers that you should check:

  1. Do you already have the model you are calling? If you’re gonna follow the tutorial, you should have a table named ‘location’ and create a model from it. Without this, dependent dropdown is useless. (Needless to say, the table should have values in it.)

  2. Make sure that the dropdown is inside the <form> tags.

That’s about it. I was able to create a dependent dropdown.

One more thing: help yourself. Some of your questions can be answered by debugging tools like Firebug or IDE with debugger (like NetBeans). In firebug, AJAX errors will be prompted at the console tab.

Hi mac

      thank for your response.I have been facing lots of problems with dependent dropdwonlist.can you please give me the code that worked for you and please mention the database tables that you used in your model.

hi all,

I am facing problems with dependent dropdowns.I have used the following tables in my database

tbl_productcategory(id,category)

tbl_productsubcategory(id,cat_id,subcategory)

tbl_productsubsubcategory(id,cat_id,description1,subcat2_id)

i have placed the below code in productsubsubcategory view and controller files.

_form.php

<div class="row">

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


            &lt;?php


              &#036;productCategoriesArray = CHtml::listData(Productcategory::model()-&gt;findAll(),'id','category');


               echo &#036;form-&gt;DropDownList(&#036;model,'cat_id',&#036;productCategoriesArray,array('prompt'=&gt;'--select--'),


                        array(


                            'ajax' =&gt; array(


                            'type'=&gt;'POST',


                            'url'=&gt;CController::createUrl('Productsubsubcategory/DynamicSubcategory'),


                            'update'=&gt;'#'.CHtml::activeId(&#036;model,'subcat2_id'))));








        ?&gt;


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


    &lt;/div&gt;


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





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








   &lt;?php  echo &#036;form-&gt;dropDownList(&#036;model,'subcat2_id', CHtml::listData(productsubcategory::model()-&gt;findAll(), 'subcategory', 'subcategory'),array('prompt'=&gt;'--Select--') );       ?&gt;








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


    &lt;/div&gt;

controller code:

public function actionDynamicSubcategory() {

     &#036;cat_id = &#036;_POST['Productsubsubcategory']['cat_id'];


         &#036;data=Productsubcategory::model()-&gt;findAll('cat_id=:cat_id',


         array(':parent_id'=&gt;(int) &#036;_POST['cat_id']));





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


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


            {


                echo CHtml::tag('option',


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


            }





    }

when i run the application and select an item in firstdropdown nothing is happened.is there any problem with my view code.how to call the function that i defined in the controller.please help me

thanks

As I already wrote you in one of the threads where you posted about dropdowns… you would need to use firefox / firebug to see if the ajax call has been made… and what is returned…

Did you try that?

i have seen it with firebug.It shows an empty console window so ajax request is not deing done?is it any syntax error?please help

check the HTML source to see if there is the javascript code… Is javascript enabled?

OOOPPSSS…

Just noticed one thing…

your code:




echo $form->DropDownList($model,'cat_id',$productCategoriesArray,array('prompt'=>'--select--'),

                            array(

                                'ajax' => array(

                                'type'=>'POST',

                                'url'=>CController::createUrl('Productsubsubcategory/DynamicSubcategory'),

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



you are passing here 5 parameters, check the documentations for dropDownList, it has only 4 parameters - http://www.yiiframew…DownList-detail

so it seems that by looking at the HTML source you would not see the javascript code that should make the AJAX call…