Dependent Dropdown: $_POST empty?

With great interest I have tried the Dependent Dropdown tutorial. Unfortunately I am stuck. The $_POST[‘Current-Controller’][‘country_id’] is always empty or not set. So I assumed that I typed a wrong controller name. It’s name is OrderController.php and the class’ name OrderController. So I tried the following variations always returning an empty or unset $_POST:

  • $_POST[‘OrderController’][‘country_id’]
  • $_POST[‘order’][‘country_id’]
  • $_POST[‘Order’][‘country_id’]
  • $_POST[‘country_id’]

Here is the states.php file which is my view file:


<div class="form" style="width:100%;">


<?php

echo CHtml::dropDownList('country_id','', array(1=>'USA',2=>'France',3=>'Japan'),

    array(

        'ajax' => array(

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

            'url'=>CController::createUrl('order/dynamicstates'), //url to call.

            //Style: CController::createUrl('currentController/methodToCall')

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

            //'data'=>'js:javascript statement' 

            //leave out the data key to pass all form values through

            )

        )

); 

 

//empty since it will be filled by the other dropdown

echo CHtml::dropDownList('city_id','', array());

?>


</div><!-- form -->

This is from the controller file, OrderController.php:


	public function actionStates()

	{

        $this->render('states');

	}

	

	public function actionDynamicstates()

	{

        $data = array($_POST['Current-Controller']['country_id']=>'This is a Test');


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

        {

            echo CHtml::tag('option',

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

        }

	}

If I replace this line…


$data = array($_POST['Current-Controller']['country_id']=>'This is a Test');

…with the following line…


$data = array(1=>'This is a Test');

…then I get at least the ‘This is a Test’ displayed in the dropdown field.

So I am guessing that my $_POST is empty?! With firebug I get Error# 500 if I use the code as posted. If I use the 2nd line, where $_POST is not used there are no errors.

Do you have any idea what I am missing here?

try this




public function actionDynamicstates()

        {

        $data = array($_POST['Current-Controller']['country_id']=>'This is a Test');

        echo "<select>";

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

        {

            echo CHtml::tag('option',

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

        }

        echo "</select>";

        }



This doesn’t work either. The select field is already in the view created by the line:


echo CHtml::dropDownList('city_id','', array());

adding the proper name to echo “<select name=‘something’>”;?

This doesn’t make any difference either :(


$data = array($_POST['Current-Controller']['country_id']=>'This is a Test');

  • when you render that action this $_POST wouldn’t make sense as there is actually no post. You should substitute it with something else that has a value

I see, but isn’t this what the tutorial suggested? I thought this is how I can retrieve the value of country_id.

How else would I get the value of the country_id dropdown field?

Hmmm, I do not really get why my version is not working?!

Is this tutorial working for everybody else? :)

If it is working for you could you please post your MVC files so I could compare what the differences are?

I assume that in my case $_POST[‘Current-Controller’][‘country_id’] is empty and does not get populated for some reason.

Do you know of any good yii-AJAX tutorials?

Maybe I am simply missing something really basic here…

I realize no one’s posted in awhile, but I’ve just been examining dependent dropDownLists today (and the aforementioned tutorial in particular) and I’ve been having the exact same issues that jrn described. Granted, I’ll continue to try and determine what I’m missing, but I was just wondering if anyone was able to determine what the problem was.

Thanks!

I figured it out. I’m pretty new to Yii (let alone php) so I’m slightly embarrassed, but hopefully someone will find the following useful:

Based off of jrn’s code, I made the following modifications to get it working:

First, here’s the states.php code:




<?php $form=$this->beginWidget('CActiveForm'); ?>


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

    array(0=>'Countries:', 1=>'USA', 2=>'France', 3=>'Japan'),

    array(

        'ajax'=>array(

            'type'=>'POST',

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

            'update'=>'#city_id', 

        )

    )

); 

 

echo CHtml::dropDownList('city_id','', array()); ?>


<?php $this->endWidget(); ?>



And here’s my controller code:




public function actionStates()

{

	$this->render('states');

}

	

public function actionDynamicstates()

{

	$country_id=(int)$_POST['country_id'];


	if ($country_id==0)

		$data=array(0=>'States:');

	else if ($country_id==1)

		$data=array(1=>'Alaska', 2=>'California');	

	else if ($country_id==2)

		$data=array(1=>'Orleans', 2=>'Bordeaux');

	else if ($country_id==3)

		$data=array(1=>'Hokkaido', 2=>'Okinawa');


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

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

}



No need to setup and populate a database table to test it out.

Also, remember to set the access rules so that both controller functions are accessible.

Glad to hear you figured it out as well, busyslacker.

I moved my code from actionDynamicStates to actionStates in order to make it work.

Also, I wrapped the code within the controller inside the following:


if(Yii::app()->request->isAjaxRequest)

{

// Your code here

}

Simply to make sure that it is a true AJAX request.