Yii Framework Forum: Populating Textfield Based On Contents From Another Textfield - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Populating Textfield Based On Contents From Another Textfield Rate Topic: -----

#1 User is offline   Yoosuf 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 23-January 13

Posted 30 January 2013 - 02:03 AM

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
0

#2 User is offline   Shahzad 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 16-March 12
  • Location:Islamabad, Pakistan

Posted 30 January 2013 - 02:52 AM

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.

2. 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 :)
0

#3 User is offline   PeRoChAk 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 578
  • Joined: 26-November 10
  • Location:Lahore, Pakistan

Posted 30 January 2013 - 02:54 AM

Use Ajax as
$(document).ready(function(){
var pid=$('#patientid').val();
$.ajax({
type:"post",
data:{pid:pid},
url:'<?php echo Yii::app()->createUrl('site/getnic')?>',
success:function(data){
$('#nicid').text(data);
}
});
});

Controller code
function actionGetNic(){
$pid=(int)$_POST['pid'];
$p=Patient::model()->findByPk($pid);
if($p){
echo $p->nic;
}
}

Cheers
Perochak
Web Design - Logo Design - Application Development

My Blog | Forums Comparison
0

#4 User is offline   Yoosuf 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 23-January 13

Posted 30 January 2013 - 03:33 AM

View PostPeRoChAk, on 30 January 2013 - 02:54 AM, said:

Use Ajax as
$(document).ready(function(){
var pid=$('#patientid').val();
$.ajax({
type:"post",
data:{pid:pid},
url:'<?php echo Yii::app()->createUrl('site/getnic')?>',
success:function(data){
$('#nicid').text(data);
}
});
});

Controller code
function actionGetNic(){
$pid=(int)$_POST['pid'];
$p=Patient::model()->findByPk($pid);
if($p){
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
0

#5 User is offline   PeRoChAk 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 578
  • Joined: 26-November 10
  • Location:Lahore, Pakistan

Posted 30 January 2013 - 10:42 AM

have you check the ids of the input fields? It was just an example code with dome ids.
Cheers
Perochak
Web Design - Logo Design - Application Development

My Blog | Forums Comparison
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users