Nested ajax dropdownlist

Elaborating…



<?php


// part of the country dropdownlist definition


      ...


      'success'=>'js:'.


        'function(html){$("#city_id").html(html); '.


        '$("#selected_country").attr("innerHTML", $("#country_id :selected").val()); '.


        '$("#city_id option:first").before("<option value='empty'>--Select City--</option>"); '.


        '$("#city_id option:first").attr("selected", "selected"); '.


        '$("#city_id").removeAttr("disabled"); '.


        '}',


        ...


// optional presentation


echo 'You selected: <span id="selected_country">nothing yet</span>';


?>




<?php


// part of the city dropdownlist definition


    ...


    'empty'=>'--Select City--',


    'disabled'=>'true',


    'ajax' => array(


   ...


?>


Feel free to optimize, test with different browsers, consider what would happen if the server becomes temporarily unavailable. I haven't tested the latter.

/Tommy

I got it to work! But need help where the code shud be appropriately placed.

  1. in my view/site/index.php, I have


                <?php


                   // Shud be in moved WHERE?


                   $criteria=new CDbCriteria;


                   $criteria->select = "ansi_code, state_name";


                   $criteria->order = "state_name asc";


                   $states=State::model()->findAll($criteria);


                ?>





                <?php echo CHtml::form('','get'); ?>


                State: 


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


                                               isset($_GET['ansi_code'])?(int)$_GET['ansi_code']:0,


                                               CHtml::listData($states,'ansi_code','state_name'),


                                               //array('empty'=>'All categories', 'submit'=>'')); 


                                               array(


                                                  'empty' => 'Select State'


                                                 ,'ajax'  => array(


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


                                                                ,'url'=>'index.php?r=city/citiesinstate' //url to call


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


                                                                ,'success'=>'js:function(html){jQuery("#feature_id").html(html);}', //selector to fill


                                                ))); 





                      // State has many Cities - empty since it will be filled by the other dropdown


                      echo CHtml::dropDownList('feature_id','',array('empty'=>'Select State First',));               


                ?>


  1. in controllers/CityController.php


<?php


public function actionCitiesinstate()


{   //print_r($_POST);


	$criteria=new CDbCriteria;


    $criteria->select = "feature_id, feature_name";


    $criteria->order = "feature_name asc";


    $criteria->condition='ansi_code='.$_POST['ansi_code'];


    $data=City::model()->findAll($criteria);


    //$data=City::model()->findAll('ansi_code=:ansi_code');


	$data=CHtml::listData($data,'feature_id','feature_name');


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


    {


        echo CHtml::tag('option',


                   array('value'=>$value),CHtml::encode($name),true);


    }


	die();


} 


It works but the code placements are not right. Learning.

Is it possible to make two ajax call from the master selection. One to show the cities and other to get detail content about the country.

Sorry, have been away from the forum for a couple of weeks.

I don't know which is the best way to initiate two Ajax calls (not an expert).

Maybe the first update can fire the second call? Just a thought.

Or one call returning both state details and city list?

/Tommy