[SOLVED]Autocomplete TextField


i’m confused to make a autocomplete in my program.

this is my case

i have 1 "dropdownlist" and then when then dropdown have choosen. i want some textfield updated with values from database.

this my code


$options = array (








            echo CHtml::dropDownList('code_level','', CHtml::listData(Tripexpense::model()->findAll(), 'id', 'level'),$options);


public function actionTripExpense()




        echo CHtml::tag('input',array('type'=>'text','id'=>'uang_makan1','value'=>$data['money']));

        echo CHtml::tag('input',array('type'=>'text','value'=>$data['airport_tax']));


when i run it, after i have change the dropdownlist. the value out in textfield #a. but i want the second value insert into #b.

need help

thank you

I think you can use JSON for them, as described in this comment. It may be a bit different, but the idea is same.

Thank’s rei for the respon.

i have read and try to convert the idea to what i need. but still not run.

i use json, but when i make it use json, there is no value that i get.

any idea…

sory for my bad english

what is the code you tried?

In above example the code ‘update’=>’#a,’#b’ will not work, you need to use ‘success’=>‘function(data){ <process the data> }’

Check again the link given by rei for an example…

yup, i have try and make like that.




and in controller i make

echo CJSON::encode(array(

                    'ume' =&gt; &quot;200&quot;,

                    'use' =&gt; &quot;300&quot;,


but still not display the value

NOTE: when you post code use the code directive (<> on the editor toolbar) so that your code is more readable.

This code seems OK, now you need to find what is wrong… check with firebug if the ajax call is made… if and what data is returned… if you have on the form some elements with id "um" and "us"…


when you post your code snippets, would make sense to post the exact copy/paste from your code… as maybe you have some error there… for example in your above code ‘updateFields …’ would make an JS error as you need to use a function there…is that your exact code ?

this my code

in view

<script type="text/javascript">

    function hpiCheck()



        echo CHtml::ajax(array(








                $('#um div.divForForm form').val(data.ume);

                $('#us div.divForForm form form').val(data.use);

            } ",


//        alert('come in');

        return false;




echo CHtml::dropDownList('code_level','id', CHtml::listData(Tripexpense::model()->findAll(), 'id', 'level'),

                                            array('empty'=>'Choose Level','onchange'=>'{ hpiCheck(); }' ));


<div id="um">


echo $form->textField($model,'uang_makanE',array('disabled'=>true));

echo $form->error($model,'uang_makanE');



<div id="us">


echo $form->textField($model,'uang_sakuE',array('disabled'=>true));

echo $form->error($model,'uang_sakuE');



in controller

public function actionTripExpense1()





                if($id != '')



                    echo CJSON::encode(array(

                        'status'=>'level complete',







but it still not work…


Lets go by steps…

Did you check with a tool like firebug that the ajax call has been made ?

Did you check what data this call returns ?

After you confirm that the call is working and that you are getting the data you need… you can work on your code to find what is wrong there…

From what is see… you are updating the #list_expenses… but the are no elements in your HTML at least in the code above…

After that you are updating $(’#um div.divForForm form’) and $(’#us div.divForForm form form’)… that elements does not exist too in your code above…

Before continuing spend some time on reading about jQuery selectors and try to understand them… begin with this page - http://api.jquery.com/id-selector/

Thank you sir

it’s work now, after i check the id textfield using firebug. after i get the id, then i put it to $(’#um’).val(data.ume)

once more, thank you sir…