Question

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

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.

[SOLVED :: Updated the CODE ] Enjoy Coding

Was it helpful?

Solution

You don't want to pass the parameter as per below example. Current element value will be taken as parameter to controller through 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'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top