Yii Framework Forum: [SOLVED]Autocomplete TextField - Yii Framework Forum

Jump to content

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

[SOLVED]Autocomplete TextField Rate Topic: -----

#1 User is offline   Anro 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 11-May 11

Posted 03 November 2011 - 02:38 AM

hello

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

view
$options = array (
'id'=>'level',
'ajax'=>array(
'type'=>'POST',
'url'=>CController::createUrl('Bussinesstrip/tripExpense'),
'update'=>'#a,#b'),
)
);
echo CHtml::dropDownList('code_level','', CHtml::listData(Tripexpense::model()->findAll(), 'id', 'level'),$options);

controller
public function actionTripExpense()
{
$id=$_POST['code_level'];
$data=Tripexpense::model()->findByPk($id);

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
0

#2 User is offline   rei 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 323
  • Joined: 10-November 10

Posted 05 November 2011 - 12:57 AM

I think you can use JSON for them, as described in this comment. It may be a bit different, but the idea is same.
Fipick - Find and pick recommendations
0

#3 User is offline   Anro 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 11-May 11

Posted 09 November 2011 - 09:05 AM

View Postrei, on 05 November 2011 - 12:57 AM, said:

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
0

#4 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,338
  • Joined: 12-October 09
  • Location:Croatia

Posted 09 November 2011 - 09:11 AM

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...
Find more about me.... btw. Do you know your WAN IP?
0

#5 User is offline   Anro 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 11-May 11

Posted 09 November 2011 - 09:19 AM

View Postmdomba, on 09 November 2011 - 09:11 AM, said:

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.

'success'=>'updateFields
$('#um).val(data.ume);
$('#us).val(data.use);
'

and in controller i make
echo CJSON::encode(array(
'ume' => "200",
'use' => "300",
));

but still not display the value
0

#6 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,338
  • Joined: 12-October 09
  • Location:Croatia

Posted 09 November 2011 - 09:28 AM

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"...

btw

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 ?
Find more about me.... btw. Do you know your WAN IP?
0

#7 User is offline   Anro 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 11-May 11

Posted 09 November 2011 - 09:40 PM

View Postmdomba, on 09 November 2011 - 09:28 AM, said:

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"...

btw

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()
    {
        <?php
        echo CHtml::ajax(array(
            'url'=>CController::createUrl('TripExpense1'),
            'data'=>array('level'=>'js:$(\'#code_level\').val()'),
            'type'=>'post',
            'dataType'=>'json',
            'success'=>"function(data)
            {
                $('#list_expense').html(data.status);
                $('#um div.divForForm form').val(data.ume);
                $('#us div.divForForm form form').val(data.use);
            } ",
            ))?>;
//        alert('come in');
        return false;
    }
</script>

<?php
echo CHtml::dropDownList('code_level','id', CHtml::listData(Tripexpense::model()->findAll(), 'id', 'level'),
                                            array('empty'=>'Choose Level','onchange'=>'{ hpiCheck(); }' ));
?>

<div id="um">
<?php
echo $form->textField($model,'uang_makanE',array('disabled'=>true));
echo $form->error($model,'uang_makanE');
?>
</div>
<div id="us">
<?php
echo $form->textField($model,'uang_sakuE',array('disabled'=>true));
echo $form->error($model,'uang_sakuE');
?>
</div>


in controller
public function actionTripExpense1()
        {
            if(Yii::app()->request->isAjaxRequest)
            {
                $id=$_POST['code_level'];
                if($id != '')
                {
                    $data=Tripexpense::model()->findByPk($id);
                    echo CJSON::encode(array(
                        'status'=>'level complete',
                        'ume'=>$data['uang_makanE'],
                        'use'=>$data['uang_sakuE']
                    ));
                    Yii::app()->end();
                }
            }


but it still not work..
0

#8 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,338
  • Joined: 12-October 09
  • Location:Croatia

Posted 10 November 2011 - 03:43 AM

OK...

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/

Find more about me.... btw. Do you know your WAN IP?
1

#9 User is offline   Anro 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 11-May 11

Posted 10 November 2011 - 08:17 AM

View Postmdomba, on 10 November 2011 - 03:43 AM, said:

OK...

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..
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