Dependent Dropdown List

Hi Everybody,

I have 2 areas in a form and every area has 6 service points. Areas and services points are static but dependent. I want to show the service points dropdown list depending on the area is selected.

i.e. Area 1 has 6 service points and

 Area 2 has 6 service points

Now, I want to show Area 1’s service points in the service point’s dropdown once area 1 is selected otherwise area 2’s service points will be shown if area 2 is selected.

Please help me, I have tried a lot but couldn’t. Let me know if my explanation is vague.

Kind regards,

Shimul

Hi,

please see it…

http://www.yiiframework.com/wiki/429/an-easy-solution-for-dependent-dropdownlist-using-ajax/

i hope it’s some help…!


<div class="row">

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

	<?php echo CHtml::dropDownList('areaId','',

           array('prompt'=>'Select Type','1'=>'area1','2'=>'area2','0'=>'Other'), 

	   array('ajax' => array('type'=>'POST',

                                   'url'=>CController::createUrl('myActionName'),

                                   'update'=>'#service'))); ?>

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

     </div>	

	

     <div class="row">

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

		<?php echo CHtml::dropDownList('service','',array()); ?>

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

      </div> 

and in controller make method myActionName() like this…




public function actionMyActionName()

{

    		$data=  Service::model()->findAll('areaserviceId=:areaId', 

                array(':areaId'=>(int) $_POST['areaId']));

 		

    		$data=CHtml::listData($data,'serviceId','serviceName');

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

	    	{

        		echo CHtml::tag('option', 

          	               array('value'=>$value), 

                             CHtml::encode($serviceName),true);

    		}

}

hope it may help you… :)

Thanks Ankit and Kalpit.

I did same thing what this tutorial says and also Kalpit’s code. But it doesn’t work for me. I wrote as :




        <div class="row">

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

        <?php

        echo CHtml::dropDownList('siteId','',

            array(1=>'Site 1',2=>'Site 2'),


            array(

                'prompt'=>'Select Site',

                'ajax' => array(

                    'type'=>'POST',

                    'url'=>CController::createUrl('servicePoint'),

                    'update'=>'#service_point',

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

                )));

        ?>

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

  	</div>


	<div class="row">

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

                <?php echo CHtml::dropDownList('service_point','',array()); ?>

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

	</div>



and in controller:




    public function actionServicePoint(){

        $models = Sp::model()->findAll('site_id=:id',

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

        $list = CHtml::listData($models,

            'id', 'name');

        return $list;


    }



Please could you check please and let me know where I am doing wrong. Please help me.

Thank in advance,

why you are using return $list… :blink:

you are doing ajax request from form and from function you are returning array…

i think for ajax request either you have to use echo or alert… :unsure:

so use echo $list instead of return $list…

Sorry Kalpit,

I pasted a wrong code. Actual code what I tried that is,

In the form:




<div class="row">

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

        <?php

        echo CHtml::dropDownList('siteId','',

            array(1=>'Site 1',2=>'Site 2'),


            array(

                'prompt'=>'Select Site',

                'ajax' => array(

                    'type'=>'POST',

                    'url'=>CController::createUrl('servicePoint'),

                    'update'=>'#service_point',

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

                )));

        ?>

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

	</div>


	<div class="row">

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

        <?php echo CHtml::dropDownList('service_point','',array()); ?>

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

	</div>



In the controller:




public function actionServicePoint(){

        $data = Sp::model()->findAll('site_id=:id',

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


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

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

        {

            echo CHtml::tag('option',

                array('value'=>$value),

                CHtml::encode($name),true);

        }


    }



Sorry, for this mistake. But this way does not work.

i think you are making some mistake in the following code…




 $data = Sp::model()->findAll('site_id=:id',

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



verify once this code…

because which code i have given above that i used in my project and it’s working fine for me…

just check id value which you are checking in controller method…

Actually my ajax request does not work. I echoed something in the action method but did not find any response. Is there any way to fix it?

why you kept prompt inside ajax array… :blink:

and have you given permission inside accessRules()?

Many many many ;D ;D ;D thanks Kalpit. It works but unfortunately data is not saved in the table. Please could you tell me how to fix it.

:D

Glad to know :huh:

What was the issue?

Only this ajax field data is not getting saved or all fields data?

Thanks to reply. Both field’s data, calling field as well as ajax field data.

Please help me.

:)

for that write in controller create() method…




if(isset($_POST['your_controller_name']))

{

     $model->siteId=$_POST['siteId'];

     $model->service_point=$_POST['service_point'];

}



You can try cascadedropdown?

a lot of thanks Kalpit :D :D :D , You did a big afford for me.

I understood the logic of posting the site and service point and place them into the create action properly.

Thanks again my friend. ^_^

finally done :D

Cheers … :)

+1 from my side

Yes done but thinking about for update. Could you advise me what should I do for update. ???

same thing you can apply for update also…


$model->siteId=$_POST['siteId'];

     $model->service_point=$_POST['service_point'];

write above code inside actionUpdate() method. it will work…

Hi there,

not sure if my solution is the best (had similar problem). But you could check it:

http://www.yiiframework.com/forum/index.php/topic/46584-dynamic-dropdown-on-many-to-many-tables/

Hope code is self explaining.

Good luck

Hi Kalpit actually I am worried to show data as selected in site and service point once I created.

For example, I created a form where I selected "Site 1" in Site field and "Service Point 1" in Service point field. Now my issues is when I want to update this data, I want them as selected "Site 1" in Site field and "Service Point 1" in Service point field. At this moment, I got the fields as "create form"

Please let me know how to do this thing.

Thank you

Thanks Dulo to reply here. I am testing your reference.