donnut
(Yii)
May 21, 2010, 8:34am
1
Hi,
I would like to use the current value of activeTextBox in an ajax call. The code I have so far is:
...
<?php echo CHtml::activeTextField($model,'postcode',array(
'onkeypress'=>CHtml::ajax(array(
'dataType'=>'json',
'data'=>'q='. <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />, <------------------ here the current contents of activeTextField
'success'=>'function(data,status){ ... }',
))
)); ?>
...
I have no idea how to proceed. I tried CHtml::value() without success. Or should I construct a javascript function using getElementByID?
Any help is appreciated.
Shiki
(Shikishiji)
May 21, 2010, 9:42am
2
Get it from $model
<?php echo CHtml::activeTextField($model,'postcode',array(
'onkeypress'=>CHtml::ajax(array(
'dataType'=>'json',
'data'=>'q='. urlencode($model->postcode),
'success'=>'function(data,status){ ... }',
))
)); ?>
donnut
(Yii)
May 21, 2010, 9:51am
3
Thanks for the reply, Shiki.
Unfortunately, the resulting query stays empty. On each key press Firebug reports:
GET: http://localhost/hftt/index.php?r=addresses/postcodeLookup&_=1274435155476&q=
mdomba
(Maurizio Domba Cerin)
May 21, 2010, 10:02am
4
if you need the current value of the filed eg. the value just typed in… you have to use javascript… PHP does not know that value until you submit…
first find the ID of the INPUT filed it should me something like modelname _postcode
than you can use:
...
'data'=>array(
'q'=>"js:$('#modelname_postcode').val()",
),
...
donnut
(Yii)
May 21, 2010, 10:15am
5
Thanks mdomba.
It works… almost. There is a ‘timelag’ of one character. So after typing “1234” the query reports “q=123”.
Is there some way to include the last typed character as well?
zaccaria
(Matteo Falsitta)
May 21, 2010, 10:19am
6
mdomba is right, this is the correct approach to the problem.
A small improvement can be use CHtml::activeId() instead of looking for the id in the page.
So the result code looks like:
<?php echo CHtml::activeTextField($model,'postcode',array(
'onkeypress'=>CHtml::ajax(array(
'dataType'=>'json',
'data'=>'q='. "js:$('".CHtml::activeId($model,'postcode')."').val()",
'success'=>'function(data,status){ ... }',
))
)); ?>
mdomba
(Maurizio Domba Cerin)
May 21, 2010, 10:20am
7
it’s because you are geting the value on key press… so the new key value is not yet there… try the keyup or change event… you are looking for an event that happens after the new key value is added to the input field…
donnut
(Yii)
May 21, 2010, 10:22am
8
Thanks zaccaria for the usefull addition.
I changed ‘onkeypress’ to ‘onkeyup’ and now it works as required.
Thanks again everyone!
mdomba
(Maurizio Domba Cerin)
May 21, 2010, 10:37am
9
Thanks zaccaria, I was looking for something like that, but couldn’t find it
Just to add two cents more for those that will read this thread
you need to add a # before CHtml… like
<?php echo CHtml::activeTextField($model,'postcode',array(
'onkeypress'=>CHtml::ajax(array(
'dataType'=>'json',
'data'=>'q='. "js:$('#".CHtml::activeId($model,'postcode')."').val()",
'success'=>'function(data,status){ ... }',
))
)); ?>
slabonart
(Slabonart)
June 28, 2014, 6:37am
10
I was looking for this THANKS