Populating Textfield Based On Contents From Another Textfield

I need to populate a textfield automatically based on the input of another textfield.

The scenario is,

<?php echo $form->textField($model,'patientId',array('size'=>21.5,'maxlength'=>10)); ?>

<?php echo $form->textField($model,'Nic',array('size'=>21.5,'maxlength'=>10)); ?>

The NIC must get populated (from the database) according to the patientID textfield input, or vice versa

Any ideas of how this could be accomplished

It can be done in several ways, for example:-

  1. Add a submit button to post back data of patient id to your controller (dont forget to include fields in form)

1a. Alternatively if you dont want do display a submit button, then you can use a javascript event for the textfield for example keyup etc to postback your data to controller.

  1. controller then finds the NIC based on patient id using model, and render the same page to display both patient id and nic.

given you food for thought :)

Use Ajax as


var pid=$('#patientid').val();




url:'<?php echo Yii::app()->createUrl('site/getnic')?>',






Controller code

function actionGetNic(){




echo $p->nic;



Thanks, I have one more query. I tried this out but it does not work. I included the ajax code in the view _form.php file at the top under the <script> tag

have you check the ids of the input fields? It was just an example code with dome ids.