Get Value Of Second Dropdown

Hi,

I am playing with Yii for a few days and so far I am very impressed. I am having fun :)

At this moment I am playing with dependent dropdown lists, it all works great until the point that I want to get the value of the second drop down.

in my view: (I am working with a textfield instead of a dropdown, the entry will be searched in the database and works flawlessly)


echo CHtml::textField("input_town",'',array('id'=>'input_town','ajax' => array(

                        'type' =>'POST',

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

                        'update' => '#townlist',

                        'data' => 'html',

                        'submit' => '',

        'data'=>"js:{input_town: $('#input_town').val()}",

                ))); ?>

In my controller I have :


    public function actionTown() {

        $a = $_POST["input_town"];

        ...

        ...

        looks in database etc.

        ...

        echo CHtml::dropDownList('test','', 

            array(2=>'test',1=>'Middle Atlantic',3=>'East North Central'),

            array(

            'ajax' => array(

            'type'=>'POST',

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

            'update'=>'#showing_id',

       )));

    }

The dropdown is put exactly where I want it to bel And as you see I am trying to put a bit of AJAX in it hoping that it will do the same as the code in my VIEW. However it is not working.

When I check in firebug I see the controller working perfectly when I type text and hit enter. However nothing when I make a selection in my dropdownlist. I also played with ‘onChange’ but also not the desired results.

How do I get the value of the second dropdown?

Thanks in advance,

W.

Try to debug step by step first.

first echo the value of $a, then print the results from database, and later check the result of CHtml::dropdownlist;

I did already, I forgot to mention that I have in my controller the following;


     public function actionTowna() {

      $a = $_POST["region"];   

      print $a;

     }

It is not being called at all. I use netbeans and firebug to debug

rgds,

W

check the path


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

this too




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



echo something here ,in actionKill

actionTowna() or actionTown() ?

The textfield in views is calling actionTown.

the dropdownlist in my controller is calling actionTowna

The created URL with Kill should be ignored. that’s just a typo from my side.

so whats happening?

Nothing, the controller ‘towna’ is not being called. It is weird though because when I copy paste the code below into my view it is working flawlessly.


echo CHtml::dropDownList('film_id','', 

            array(2=>'test',1=>'Middle Atlantic',3=>'East North Central'),

        array(

        'ajax' => array(

        'type'=>'POST',

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

        'update'=>'#showing_id',

))); 

Ok, got it working. My approach was wrong.

in my view:


echo CHtml::textField("input_town",'',array('id'=>'input_town','ajax' => array(

                        'type' =>'POST',

                        'url' => CController::createUrl('data/town'),

                        'update' => '#idDistrict',

                        'data' => 'html',

                        'submit' => '',

        'data'=>"js:{input_town: $('#input_town').val()}",

                ))); 


echo CHtml::listBox('idDistrict','', array(), array('prompt'=>'Select District','size' => '5',

                        'ajax' => array(

                        'type' =>'POST',

                        'url' => CController::createUrl('data/towna'),

                        'update' => '#townlist',

                        'data' => 'html',

                        'submit' => '',

        'data'=>"js:{idDistrict: $('#idDistrict').val()}",

                )));

And in my Controller:


    public function actionTown() {

        $a = $_POST["input_town"];


	$res =array();


	if (isset($_POST['input_town'])) {

            $criteria_user = $_POST['input_town'];

            $criteria = new CDbCriteria;

            //$criteria->condition='product=:post_id';

            //$criteria->params=array(':post_id'=>$criteria_user );

            $criteria->addSearchCondition('product', $criteria_user );

            $criteria->limit = 10;

            $post = Data::model()->findAll($criteria);


            $ld=CHtml::listData($post,'id', 'product');

            //$ld = $post;

            //$data = $post;

	}  

        

    foreach($ld as $value=>$name)    {

        echo CHtml::tag('option',

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

      }


    }        


     public function actionTowna() {

         print_r ($_POST);

     }

Now it works flawlessly.