Yii2: auto fill fields based on another field from related table
ajax, mysql, php, yii2
Solution
All you need is calling an `AJAX` request to get needed fields. Just act like below:
(I do not know your model name)take a look at your form and see what is the `id` of your `patient_name` field. It is usually `modelname-fieldname`. I assume that your model name is `Patient`. So, the id of `patient_name` would be `patient-patient_name`.
Add an ajax request (in your view).
The code for calling AJAX could look just like below:
$this->registerJs("$('#patient-patient_name').on('change',function(){
$.ajax({
url: '".yii\helpers\Url::toRoute("controllerName/patient")."',
dataType: 'json',
method: 'GET',
data: {id: $(this).val()},
success: function (data, textStatus, jqXHR) {
$('#patient-city').val(data.city);
$('#patient-state').val(data.state);
},
beforeSend: function (xhr) {
alert('loading!');
},
error: function (jqXHR, textStatus, errorThrown) {
console.log('An error occured!');
alert('Error in ajax request');
}
});
});");
Notes:
- Change the ControllerName in above code with your own.
- I assumed that the id of `city` and `state` fields has the following id(s): `patient-city` and `state-city` relatively.
- patient is an action in your controller
- You may need to remove alerts|logs and do some customization on above code
I didn't consider any conditions for code cleaning. Please make sure that user data is correct.
- Finally, add action code into your controller.
Action code:
public function actionPatient($id){
// you may need to check whether the entered ID is valid or not
$model= \app\models\Patient::findOne(['id'=>$id]);
return \yii\helpers\Json::encode([
'city'=>$model->city,
'state'=>$model->state
]);
}
Problem
I have a MySQL table and model `patient_entry` which contains fields `patient_name`, `city` and `state`. I also have another table/model `health_card` which also contains `patient_name`, `city` and `state`. Suppose the `patient_entry` table is already filled with `patient_name`, `city` and `state`. When I am entering data in `health_card` form, when I select the `patient_name` via drop-down field related to `patient_entry` table, I want the related `city` and `state` fields to be auto-filled. My `_form.php` for `health_card` looks like this: ``` <head> <script> $this->registerJs("$('#healthcard-patient_name').on('change',function(){ $.ajax({ url: '".yii\helpers\Url::toRoute("HealthCard/patient")."', dataType: 'json', method: 'GET', data: {id: $(this).val()}, success: function (data, textStatus, jqXHR) { $('#healthcard-city').val(data.city); $('#healthcard-pincode').val(data.pin); }, beforeSend: function (xhr) { alert('loading!'); }, error: function (jqXHR, textStatus, errorThrown) { console.log('An error occured!'); alert('Error in ajax request'); } }); });"); </script> </head> ``` And in the controller I have added, as per the suggestion, this: ``` public function actionPatient($id){ // you may need to check whether the entered ID is valid or not $model= \app\models\PatientEntry::findOne(['id'=>$id]); return \yii\helpers\Json::encode([ 'city'=>$model->disrict_city, 'pin'=>$model->pin_code ]); } ```