3

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.

3
  • add html part also for this $('#dailywardentry-room_name') Commented Dec 17, 2014 at 4:12
  • Hi, Nitish Kumar - I have already added this - see the response I am getting in firefox -**GET http://localhost/hospitalerp/web/index.php?r=daily-ward-entry%2Fcharges-cash&id=1&room_category=6** Commented Dec 17, 2014 at 4:19
  • I think the problem is in the code which needs to be modified - $model = \app\models\IpdCharges::findOne(['id'=>$id]); Here only id is used in find and I don't know how to add parameter for room_category Commented Dec 17, 2014 at 4:23

1 Answer 1

3

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
}
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Ali, Thanks for your suggestion - But I am getting a 500 internal server error in firebug An error occured! index.p.../create (line 569) GET http://localhost/hospitalerp/web/index.php?r=daily-ward-entry%2Fcharges-cash&id=1&room_category=6 I have tried both methods
I have tried a var_dump on $room_category=Yii::$app->getRequest()->getQueryParam('room_category'); var_dump($room_category);exit;` I am getting null in the dump result
Hi Ali, Ok there is a small typo in in your second option - i.e. getQueryParam should have been getQueryParams. Now I am getting the var_dump ok, but still the ajax error.
Ok Ali variable_dump for $model I am getting null that is the cause of the problem
Hi Ali - Finally resolved with option 1, I have to make little change like $model = \app\models\IpdCharges::findOne(['doctor'=>$id,'room_category'=>$room_category]); that is replace id with 'doctor' which is the column name in ipd_charges table. Thank you so much,you are Genius!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.