anro
(Febrin Sitohang)
November 3, 2011, 7:38am
1
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
re1nald0
(Reinld17)
November 5, 2011, 5:57am
2
I think you can use JSON for them, as described in this comment. It may be a bit different, but the idea is same.
anro
(Febrin Sitohang)
November 9, 2011, 2:05pm
3
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
mdomba
(Maurizio Domba Cerin)
November 9, 2011, 2:11pm
4
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…
anro
(Febrin Sitohang)
November 9, 2011, 2:19pm
5
mdomba:
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
mdomba
(Maurizio Domba Cerin)
November 9, 2011, 2:28pm
6
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 ?
anro
(Febrin Sitohang)
November 10, 2011, 2:40am
7
mdomba:
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…
mdomba
(Maurizio Domba Cerin)
November 10, 2011, 8:43am
8
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/
anro
(Febrin Sitohang)
November 10, 2011, 1:17pm
9
mdomba:
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…