我有一个国家,州,城市表,我需要3个下拉菜单 其中,如果我选择像美国这样的国家,状态下拉菜单将自动仅显示美国下的州,接下来,如果我选择像加利福尼亚这样的州,则城市下拉菜单将仅显示加利福尼亚州的城市
我目前在yii中实现此问题。我有这个_form文件 应该包括这3个下拉列表。我有一个部分代码,但我无法弄清楚如何使这项工作
这是来自Controller
public function actionDynamicstates()
{
$sql = "SELECT StateName FROM gg_t_worldareasstates ".
"WHERE CountryID = :countryid";
$command = Yii::app()->createCommand($sql);
$command->bindValue(':countryid', $_POST['CountryID'], PDO::PARAM_INT);
$data = $command->execute();
$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>
我将上述代码更改为此代码
<div class="row">
<?php echo $form->labelEx($model,'Country'); ?>
<?php
$country = new CDbCriteria;
$country->order = 'CountryName ASC';
?>
<?php
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"
)
)
);
?>
<?php echo $form->error($model,'CountryID'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'State'); ?>
<?php
$state = new CDbCriteria;
$state->order = 'StateName ASC';
?>
<?php
echo $form->dropDownList($model,'StateID',CHtml::listData(Worldareasstates::model()->findAll($state),'StateID','StateName'),
array(
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('wsmembersdetails/dynamiccities'),
'update' => '#CityID'
)
)
);
?>
<?php echo $form->error($model,'StateID'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'CityID'); ?>
<?php echo $form->dropDownList($model,'CityID','',array());?>
<?php echo $form->error($model,'CityID'); ?>
答案 0 :(得分:1)
更改此行
$ data = $ command-&gt; execute();
到
$ data = $ command-&gt; query();
答案 1 :(得分:-2)
这个问题已经解决了,它出现在yii wiki docs
上