auto fill fields based on another field from related table

I have a MySQL table and model say patient_entry which contains fields patient_name, city and state, then I 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. Now 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 field to be automatically filled.

my _form.php for health_card form looks like this:




?php $form = ActiveForm::begin(); ?>


    <?= $form->field($model, 'health_card_number')->textInput(['maxlength' => 30]) ?>


    <?= $form->field($model, 'patient_name')

    ->DropDownList(ArrayHelper::map(\app\models\PatientEntry::find()

    ->all(), 'id', 'patient_name' ), [ 'prompt' => 'Please Select' ])?>


    <?= $form->field($model, 'city')->textInput(['maxlength' => 30]) ?>


    <?= $form->field($model, 'state')->textInput(['maxlength' => 30]) ?>



If I’m understanding you correctly, I think you should look at your data structure and see how you can develop it so you do not duplicate data. If you store the same data in two tables, it violates DRY and then what happens if you update in one place but not the other? Which one would be authoritative? Just a thought.

Evercode Thanks for your suggestion, but the functionality I want is applicable in many places, for example I store a room price in room table,which I want to pull it automatically in patient table, if needed that can be changed in specific cases if needed, otherwise leave it as it is.

Then I am giving just an example there are say twenty fields from which just five are common in two tables, which can be pulled from other table.

So my question is how it can be done? which will indeed be useful in many cases.