使用Yii中的下拉列表的Ajax Dependable texfiled无法正常工作

时间:2022-07-12 14:13:42

[SOLVED :: Updated the CODE ] There is a drop down list and a text filed. Text filed will be filled per drop down selection by Ajax in Yii form. And I need to pass parameter to controller via Ajax URL. It is working when I pass static parameter via URL. But failed to get the dynamic parameter.

[已解决::更新了代码]有一个下拉列表和一个文本字段。文本归档将由Ajax以Yii形式按下拉选择填充。我需要通过Ajax URL将参数传递给控制器​​。当我通过URL传递静态参数时,它正在工作。但未能获得动态参数。

My Form::

    <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>

My Controller::

        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;

    }

When I send parameter from Ajax URl "array('pp_store'=>'333')" then it is working well. But I need to send data dynamically.

当我从Ajax URl发送参数“array('pp_store'=>'333')”时,它运行良好。但我需要动态发送数据。

[SOLVED :: Updated the CODE ] Enjoy Coding

[已解决::更新了CODE]享受编码

1 个解决方案

#1


0  

You don't want to pass the parameter as per below example. Current element value will be taken as parameter to controller through POST.

您不希望按以下示例传递参数。当前元素值将通过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());

If you want to send data manually, then you have to uncomment the 'data'=>'js:javascript statement'.

如果要手动发送数据,则必须取消注释'data'=>'js:javascript statement'。

#1


0  

You don't want to pass the parameter as per below example. Current element value will be taken as parameter to controller through POST.

您不希望按以下示例传递参数。当前元素值将通过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());

If you want to send data manually, then you have to uncomment the 'data'=>'js:javascript statement'.

如果要手动发送数据,则必须取消注释'data'=>'js:javascript statement'。