Yii2: Ajax Call multiple parameters

2019-07-04 11:43发布

问题:

I am using this code for auto-fill with ajax call without any problem:

Code in my view file:

$this->registerJs("$('#dailywardentry-doctor_visit_name').on('change',function(){
    $.ajax({
        url: '".yii\helpers\Url::toRoute("daily-ward-entry/charges-cash")."',
        dataType: 'json',
        method: 'GET',       
        data: {id:$('#dailywardentry-doctor_visit_name').val(),
       room_category:$('#dailywardentry-room_name').val()// Want to use this additional params 

        },
        success: function (data, textStatus, jqXHR) {
            $('#dailywardentry-visit_charges').val(data.visit_charges);
            $('#dailywardentry-doctor_id').val(data.doctor_id);
            },
    });
});");

And code in my controller is like:

public function actionChargesCash($id){

        $model = \app\models\IpdCharges::findOne(['id'=>$id]);
        return \yii\helpers\Json::encode([
            'visit_charges'=>$model->charges_cash,
            'doctor_id'=>$model->doctor_id

        ]); 
    }

I want to fill the data on the basis of additional parameter room_category:$('#dailywardentry-room_name').val() which is not working.

While checking in firefox I am getting the status 200 OK like:

**GET http://localhost/hospitalerp/web/index.php?r=daily-ward-entry%2Fcharges-cash&id=1&room_category=6**

Still I am getting the values as if the parameter room_category is not supplied.

Any solution will be greatly appreciated. Thanks.

回答1:

You can get this param like below:

public function actionChargesCash($id,$room_category){
    $model = \app\models\IpdCharges::findOne(['id'=>$id,'room_category'=>$room_category]);
    //rest of code
}

There is another way to do that:

public function actionChargesCash($id){
    $room_category=Yii::$app->getRequest()->getQueryParam('room_category');
    $model = \app\models\IpdCharges::findOne(['id'=>$id,'room_category'=>$room_category]);
    //rest of code
}