Yii2: autocomplete fields based on another field from linked table
I have a MySQL table and model patient_entry
that contains fields patient_name
, city
and state
. I also have another table / model health_card
that also contains patient_name
, city
and state
.
Suppose the table is patient_entry
already full patient_name
, city
and state
.
When I enter data into the form health_card
, when I choose a field patient_name
from the pop field, referring to the table patient_entry
, I want to be related field city
and state
have been automatically filled.
Mine _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 added, as suggested, the following:
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
]);
}
source to share
All you need is to invoke a query AJAX
to get the required fields. Just proceed as follows:
-
(i dont know your model name) look at your form and see what is
id
your fieldpatient_name
. Usually it ismodelname-fieldname
. I am assuming your model namePatient
. So the identifierpatient_name
will bepatient-patient_name
. -
Add ajax request (in your opinion).
The code for the AJAX call might look like:
$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 ControllerName in the above code with your own.
- I assumed the fields id
city
andstate
have the following id (s):patient-city
andstate-city
relative. - patient is an action in your controller
- You may need to remove the notification logs and do some customization with the above code.
-
I have not considered any conditions to clean up the code. Make sure the user details are correct.
- Finally, add the action code to 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
]);
}
source to share