[已解决::更新了代码] 有一个下拉列表和一个文本字段。文本归档将由Ajax以Yii形式按下拉选择填充。我需要通过Ajax URL将参数传递给控制器。当我通过URL传递静态参数时它正在工作。但未能获得动态参数。
<div class="row">
<?php echo $form->labelEx($model,'pp_store'); ?>
<?php // echo $form->dropDownlist($model,'pp_store', CHtml::listData(Branchmaster::model()->findAll(), 'cm_branch', 'cm_branch')); ?>
<?php $storeArray = CHtml::listData(Branchmaster::model()->findAll(),'cm_branch','cm_branch');
echo $form->dropDownList($model,'pp_store', $storeArray,
array(
'empty'=>"Select Warehouse",
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('purchaseordhd/GetCurrency' ),
'update' => '#currencyid',
'data'=>array('store'=>'js:this.value',),
'success'=> 'function(data) {$("#currencyid").empty();
$("#currencyid").val(data);
} ',
),
)); ?>
<?php echo $form->error($model,'pp_store'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'pp_currency'); ?>
<?php echo $form->textField($model,'pp_currency', array('id'=>'currencyid', 'readonly'=> true)); ?>
<?php echo $form->error($model,'pp_currency'); ?>
</div>
public function actionGetCurrency()
{
$q = $_POST['store'];
$sql = "SELECT cm_currency as value FROM cm_branchmaster WHERE cm_branch= '$q' ";
$command = Yii::app()->db->createCommand($sql);
$result= $command->queryScalar();
echo $result;
}
当我从Ajax URl发送参数“array('pp_store'=&gt;'333')”时,它运行良好。但我需要动态发送数据。
[已解决::更新了代码] 享受编码
答案 0 :(得分:0)
您不希望按照以下示例传递参数。当前元素值将通过POST作为控制器的参数。
http://www.yiiframework.com/wiki/24/creating-a-dependent-dropdown/
echo CHtml::dropDownList('country_id','', array(1=>'USA',2=>'France',3=>'Japan'),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('currentController/dynamiccities'), //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());
如果您想手动发送数据,则必须取消注释'data'=>'js:javascript statement'
。