How to get instant value of activeTextField?

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.

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){ ... }',

        ))

    )); ?>

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=

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()",

        ),

...



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?

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){ ... }',

        ))

    )); ?>




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…

Thanks zaccaria for the usefull addition.

I changed ‘onkeypress’ to ‘onkeyup’ and now it works as required.

Thanks again everyone!

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){ ... }',

        ))

    )); ?>



I was looking for this :) THANKS