Two Dropdown Depend On One Dropdown

Hello,

I’ve searching through the internet for about 4 days but I still cannot figure out, my problem is any idea how to make two dropdownlist depend on one dropdownlist? I manage to do one to one. Hopefully, someone can answer my question. I’m new in ajax and yii. Any link for reference is appreciate

view




 <div class="row">

        <!-- LOCATION -->

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

        <?php echo $form->dropDownList($model,'locationid',

            CHtml::listData(location::model()->findAllbysql('select id,CONCAT(description, " (", code ,")") as description from tbllocation'), 'id', 'description'),

            array(

                'ajax'=>array(

                    'type'=>'POST',

                    'url'=> $ajaxUrl,

                    'update'=>

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

                    'beforeSend'=>'function(){

                            $("#fieldmaster_divisionid").find("option").remove();

                            $("#fieldmaster_subdivisionid").find("option").remove();

                            }'

                ),'prompt'=>'<please select>'


                /* 'ajax'=>array(

                     'type'=>'POST',

                     'url'=>CController::createUrl('fieldmaster/Selectdos3'),

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

                 ),*/

           )


        ); ?>

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

    </div>


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

        <?php echo $form->dropDownList($model,'blockid',array()); ?>

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


 <div class="row">

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

       <?php echo $form->dropDownList($model,'divisionid',array()); ?>

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

    </div>



controller




public function actionSelectdos()

    {

        $id_uno = $_POST['fieldmaster']['locationid'];

        $lista = division::model()->findAll('locationid = :id_uno', array(':id_uno'=>$id_uno));

        $lista = CHtml::listData($lista, 'id', 'description');

        echo "<option value = '0'><please select></option>";

        foreach($lista as $valor => $description){

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

        }

    }



someone?

First try to change your Ajax call update to a function.




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



I don’t have available code, but below is one example, which depends one Ajax call return data to fill two fields.




success:function(engdata){engineer=jQuery.parseJSON(engdata);

$("#ToolRequestPage1_engineer_name").val(engineer.engname);

$("#ToolRequestPage1_engineer_email").val(engineer.engemail);

}



Next, you need change your actionSelectdos function in the controller, so it can return two arrays in JSON encode after Ajax call. Below is one example tell you how to do that: Basically, it’s return an array and use Jason encoded. The only difference is you need create two arrays and merge into one return to ajax callback.




	/**

	 * Ajax call: Find a field engineer.

	 */

	public function actionFindEngineer()

	{

		$engineer_name=$_GET['term'];

		$return=array();

		if(isset($engineer_name))

		{

			$engineers = User::model()->findAll(array(

					'condition'=>'user_firstname LIKE :name OR user_lastname LIKE :name OR user_email LIKE :name',

					//'order'=>'user_firstname, user_lastname',

					'params'=>array(':name'=>trim($engineer_name).'%'),

			));

			if(!empty($engineers))

			{

				foreach($engineers as $engineer)

				{

					//Below is used for CJuiAutoComplete

					$return[]=$engineer->fullname.' ('.$engineer->user_email.')';

				}

			}

			//add IM engineer name from tool_request_form

			$additional_engineers = Contact::model()->findAll(array(

					'condition'=>'contact_firstname LIKE :name OR contact_lastname LIKE :name OR contact_email LIKE :name',

					//'order'=>'implementation_manager',

					'params'=>array(':name'=>trim($engineer_name).'%'),

					));

			$ret=$return;

			if(!empty($additional_engineers))

			{

				foreach($additional_engineers as $engineer)

				{

					$info=$engineer->fullname.' ('.$engineer->contact_email.')';

					if(!in_array($info, $ret))

					{

						$return[]=$info;

					}

				}

			}

		}

		sort($return);

		echo CJSON::encode($return);

		Yii::app()->end();

	}



Then depends on these return data, create your drop down list in Ajax update function.

I don’t know if it’s good enough to help you.

Thank you so much bro… I really appreciate… I’ve found out the solution… :)

You can share your solution to help others.

Please can you share the solution with me. I have similar problem. noblemfd@yahoo.com