[SOLVED]dependent dropdown not working

hi, what am I missing with this dependent drop down of yii ?

this is my "_form" view code




		<?php 

			  $country = new CDbCriteria; 

			  $country->order = 'CountryName ASC';

				 echo $form->dropDownList($model,'CountryID','',CHtml::listData(Worldareascountries::model()->findAll($country),'CountryID','CountryName'),

				 	array(

				 		'ajax' => array(

				 			'type' => 'POST',

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

				 			'update' => '#StateID',

				 		)

				 	)

				 );

			 	echo $form->dropDownList('CityID','', array());

		?>



and this is in the controller




	public function actionDynamicstates()

	{

	

		$data = Worldareasstates::model()->findAll('CountryID=:parent_id',

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

		

		$data = CHtml::listData($data,'StateID','StateName');

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

		{

			echo CHtml::tag('option',

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

		}

	}



I based those two code from this example

why am I getting this error ?

I’m sorry I made mistake in my post and I deleted it.

Try to remove ’ ’ after ‘CountryID’ in $form->dropDownList() in your view.

it didn’t helped.

I tried experimenting with DAO, still no luck

controller




	public function actionDynamicstates()

	{

	

		$sql = "SELECT StateName FROM gg_t_worldareasstates WHERE CountryID = :countryid";

		$command = Yii::app()->createCommand($sql);

		$command->bindValue(':countryid', $_POST['Wsmembersdetails']['CountryID'], PDO::PARAM_INT);

		$data = $command->query();

		

		$data = CHtml::listData($data,'StateID','StateName');

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

		{

			echo CHtml::tag('option',

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

		}

	

	}



_form





	<div class="row">

		<?php 

			  $country = new CDbCriteria; 

			  $country->order = 'CountryName ASC';

				 echo $form->dropDownList($model,'CountryID',CHtml::listData(Worldareascountries::model()->findAll($country),'CountryID',

				 	array(

				 		'ajax' => array(

				 			'type' => 'POST',

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

				 			'update' => '#StateID',

				 		)

				 	))

				 );

			 	echo $form->dropDownList('StateID','', array());


		

		?>

		

	</div>	



:mellow:

By the way your problem also may be in your ajax part (‘update’ => ‘#StateID’). Update may be used for divs but not for form elements. Instead you must use something like


'success'=>'js:function(data)

{

  $("#StateID").val(data);

}'

My previous post will not do. In your case you do need to use ‘update’ => ‘#StateID’.

But you must change


echo $form->dropDownList('StateID','', array());

to


echo $form->dropDownList($model,'StateID', array());

Sorry for my clumsy posts. Just trying to help. :)

And also in your view type this


$stateId = CHtml::activeId($model, 'StateID');

...

'update' => '#'.$stateId',

...



thanks for your tips, but non worked , i think it’s with the my controller code





	public function actionDynamicstates()

	{

		$country = $_POST['Wsmembersdetails']['CountryID'];

		

		$sql = "SELECT StateName FROM gg_t_worldareasstates WHERE CountryID = :countryid";

		$command = Yii::app()->createCommand($sql);

		$command->bindValue(':countryid', $_POST['Wsmembersdetails']['CountryID'] , PDO::PARAM_INT);

		$data = $command->query();

		

		$data = CHtml::listData($data,'StateID','StateName');

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

		{

			echo CHtml::tag('option',

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

		}

	

	}



question now is, where will i look for that error, how to fix this ?

Try your first controller with my suggestions.

ok i tried using your suggestions with my first controller func()




  public function actionDynamicstates()

        {

        

                $data = Worldareasstates::model()->findAll('CountryID=:parent_id',

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

                

                $data = CHtml::listData($data,'StateID','StateName');

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

                {

                        echo CHtml::tag('option',

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

                }

        }



_form




		<?php 

			  $country = new CDbCriteria; 

			  $statID = CHtml::activeId($model, 'StateID');

			  $country->order = 'CountryName ASC';

				 echo $form->dropDownList($model,'CountryID',CHtml::listData(Worldareascountries::model()->findAll($country),'CountryID',

				 	array(

				 		'ajax' => array(

				 			'type' => 'POST',

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

				 			'update' => '#'.$stateID,

				 		)

				 	))

				 );

			 	echo $form->dropDownList($model,'StateID', array());


		

		?>



doesn’t work, same screen shot error

What if you rewrite your controller like this


public function actionDynamicstates()

        {

        

                $data = Worldareasstates::model()->findAll('CountryID=:parent_id',

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

                

                foreach($data as $value)

                {

                        echo CHtml::tag('option',

                                        array('value'=>$value->StateID),CHtml::encode($value->StateName),true);

                }

        }

doesn’t work either :(

I have similar code and it works. Here it is

view


$vch_nav = CHtml::activeId($model, 'vch_nav');

echo $form->dropDownList($model,'school_id', $shkoly_list, array(

                         'empty' => '',

                         'class'=>'input input_r',

                         'style'=>'background-color:#FFFFFF',

                         'ajax' => array(

                         'type'=>'POST',

                         'url'=>CController::createUrl('site/vchytel'),

                         'update'=>"#".$vch_nav,

                         'data'=>array('school_id'=>'js:$(this).val()'),

                             ),

                         ));

echo $form->dropDownList($model,'vch_nav',array(),array('empty' => '', 'class'=>'input input_r', 

                                                                    'style'=>'background-color:#FFFFFF'));

Controller


public function actionVchytel()

{

	$school_id=$_POST['school_id'];

	$vchyteli = JosPpnz::model()->findAll('schoolid = :school_id order by name',array(':school_id'=>$school_id));

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

	foreach($vchyteli as $v)

	{

		echo CHtml::tag('option',array('value'=>$v->pedid),$v->name.' '.$v->predmet,true);

	}	

}

May be it will help you.

am almost there,and yet so far, am having trouble, as firebug told me so

here’s what the response contains




<!DOCTYPE html PUBLIC

	"-//W3C//DTD XHTML 1.0 Transitional//EN"

	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<title>

CDbException</title>


<style type="text/css">

/*<![CDATA[*/

body {font-family:"Verdana";font-weight:normal;color:black;background-color:white;}

h1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }

h2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }

h3 {font-family:"Verdana";font-weight:bold;font-size:11pt}

p {font-family:"Verdana";font-size:9pt;}

pre {font-family:"Lucida Console";font-size:10pt;}

.version {color: gray;font-size:8pt;border-top:1px solid #aaaaaa;}

.message {color: maroon;}

.source {font-family:"Lucida Console";font-weight:normal;background-color:#ffffee;}

.error {background-color: #ffeeee;}

/*]]>*/

</style>

</head>


<body>

<h1>CDbException</h1>


<h3>Description</h3>

<p class="message">

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'parent_id' in 'where clause'</p>


<h3>Source File</h3>

<p>

C:\xampp\htdocs\yii\yii\framework\db\CDbCommand.php(387)</p>


<div class="source">

<pre>

00375: 

00376:             if($this-&gt;_connection-&gt;enableProfiling)

00377:                 Yii::endProfile('system.db.CDbCommand.query('.$this-&gt;getText().')','system.db.CDbCommand.query');

00378: 

00379:             return $result;

00380:         }

00381:         catch(Exception $e)

00382:         {

00383:             if($this-&gt;_connection-&gt;enableProfiling)

00384:                 Yii::endProfile('system.db.CDbCommand.query('.$this-&gt;getText().')','system.db.CDbCommand.query');

00385:             Yii::log('Error in querying SQL: '.$this-&gt;getText().$par,CLogger::LEVEL_ERROR,'system.db.CDbCommand');

00386:             $errorInfo = $e instanceof PDOException ? $e-&gt;errorInfo : null;

<div class="error">00387:             throw new CDbException(Yii::t('yii','CDbCommand failed to execute the SQL statement: {error}',

</div>00388:                 array('{error}'=&gt;$e-&gt;getMessage())),(int)$e-&gt;getCode(),$errorInfo);

00389:         }

00390:     }

00391: }

</pre>

</div><!-- end of source -->


<h3>Stack Trace</h3>

<div class="callstack">

<pre>

#0 C:\xampp\htdocs\yii\yii\framework\db\CDbCommand.php(272): CDbCommand-&gt;queryInternal('fetchAll', 2, Array)

#1 C:\xampp\htdocs\yii\yii\framework\db\ar\CActiveRecord.php(1231): CDbCommand-&gt;queryAll()

#2 C:\xampp\htdocs\yii\yii\framework\db\ar\CActiveRecord.php(1317): CActiveRecord-&gt;query(Object(CDbCriteria), true)

#3 C:\xampp\htdocs\yii\app\protected\controllers\WsmembersdetailsController.php(88): CActiveRecord-&gt;findAll('parent_id=:pare...', Array)

#4 C:\xampp\htdocs\yii\yii\framework\web\actions\CInlineAction.php(50): WsmembersdetailsController-&gt;actionDynamiccities()

#5 C:\xampp\htdocs\yii\yii\framework\web\CController.php(300): CInlineAction-&gt;run()

#6 C:\xampp\htdocs\yii\yii\framework\web\CController.php(278): CController-&gt;runAction(Object(CInlineAction))

#7 C:\xampp\htdocs\yii\yii\framework\web\CController.php(257): CController-&gt;runActionWithFilters(Object(CInlineAction), Array)

#8 C:\xampp\htdocs\yii\yii\framework\web\CWebApplication.php(324): CController-&gt;run('dynamiccities')

#9 C:\xampp\htdocs\yii\yii\framework\web\CWebApplication.php(121): CWebApplication-&gt;runController('wsmembersdetail...')

#10 C:\xampp\htdocs\yii\yii\framework\base\CApplication.php(135): CWebApplication-&gt;processRequest()

#11 C:\xampp\htdocs\yii\app\index.php(13): CApplication-&gt;run()

#12 {main}</pre>

</div><!-- end of callstack -->


<div class="version">

2010-11-27 01:35:10 Apache/2.2.14 (Win32) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l mod_autoindex_color PHP/5.3.1 mod_apreq2-20090110/2.7.1 mod_perl/2.0.4 Perl/v5.10.1 <a href="http://www.yiiframework.com/">Yii Framework</a>/1.1.4</div>

</body>

</html>



my controller (based from the yii wiki dependent dropdown




	public function actionDynamiccities()

	{

		$data = Worldareasstates::model()->findAll('parent_id=:parent_id',

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

						

	

		$data = CHtml::listData($data,'StateID','StateName');

		foreach($data as $value)

		{

			echo CHtml::tag('option',

					array('value' => $value->StateID),CHtml::encode($value->StateName),true);

		}

	}



_form view







<?php echo CHtml::beginForm(); ?>


<div class="row">

	<?php echo CHtml::dropDownList('country_id','$select', array(1=>'USA', 2=>'France', 3=>'Japan'),

			array(

				'ajax' => array(

				'type' => 'POST',

				'url' => CController::createUrl('wsmembersdetails/dynamiccities'),

				'update' => '#StateID',		

			)

		)

	);

	?>

</div>


<div class ="row">


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

</div>




<?php echo CHtml::endForm(); ?>




what could possibly be wrong ? ( i don’t get it )

The error message states:

Did you mean CountryID?




public function actionDynamiccities()

{

  $data = Worldareasstates::model()->findAll(

    'CountryID=:parent_id',

    array(':parent_id'=>(int) $_POST['country_id'])

  );

  ...

}



/Tommy

this problem was solved a few hours back thanks for the help to all of ya.

I posted the solution at the yii wiki creating dependent dropdown

i hope others can find the solution helpful , yii yii yii :)