jsebasct
(Jsebas Ct)
April 18, 2011, 9:43pm
1
Hi there, Im a newbie… I just have seen some topics about this ( ajax update)
but all of them are about dropDownLists, please someone could give an idea how to update an textfield based on a dropDownList value using ajax?
this is my _form :
<?php echo $form->dropDownList($model,'TYPE', $model->typeOptions); ?>
<!-- and here is the label Im trying to update -->
<?php echo CHtml::textField( 'limite_down', '', array('id'=>'limite') ) ?>
twisted1919
(Serban Cristian)
April 18, 2011, 10:12pm
2
<?php echo $form->dropDownList($model,'TYPE', $model->typeOptions,array('onchange'=>"$('#limite').val($(this).val());")); ?>
<!-- and here is the label Im trying to update -->
<?php echo CHtml::textField( 'limite_down', 'silvia 1', array('id'=>'limite') ) ?>
twisted1919
(Serban Cristian)
April 18, 2011, 10:14pm
3
To get the select TEXT and not the value:
<?php echo $form->dropDownList($model,'TYPE', $model->typeOptions,array('onchange'=>"$('#limite').val($(this).text());")); ?>
<!-- and here is the label Im trying to update -->
<?php echo CHtml::textField( 'limite_down', 'silvia 1', array('id'=>'limite') ) ?>
mithila
(Mithila)
April 23, 2011, 6:49am
4
hi!! m a newbie too… and i have a similar prob. i want to create a unique job card id. the value of job card id is based on first two drop downs selected and an incremental number.
eg.first dropdown->computer is chosen,
second dropdown->software is chosen,
then the job card id should be:comp/sw/12
here,"comp" indicates that computer is chosen,
"sw" indicates software is chosen and
12 indicates that this is a 12th entry in the database for computer and software together.
job card id is a textfield which should be automatically field once both the dropdowns are selected.
please share a bit of your knowledge…
thanks…
iivano71
(Igor Zg1987)
April 25, 2011, 12:35am
5
<?php
$script= '
$("#someid").change(function{
$.ajax({
type: "GET",
url:"'.$this->createUrl('controller/action').'",
data:{val:this.value},
success: function(data){
$("#limite").html(data);
},
});
});
';
Yii::app()->clientScript->registerScript("myscriptid",$script);
?>
<?php echo $form->dropDownList($model,'TYPE', $model->typeOptions,array('id'=>'someid')); ?>
<!-- and here is the label Im trying to update -->
<?php echo CHtml::textField( 'limite_down', '', array('id'=>'limite') ) ?>
mithila
(Mithila)
April 25, 2011, 11:05am
6
<?php
$script= '
$("#someid").change(function{
$.ajax({
type: "GET",
url:"'.$this->createUrl('controller/action').'",
data:{val:this.value},
success: function(data){
$("#limite").html(data);
},
});
});
';
Yii::app()->clientScript->registerScript("myscriptid",$script);
?>
<?php echo $form->dropDownList($model,'TYPE', $model->typeOptions,array('id'=>'someid')); ?>
<!-- and here is the label Im trying to update -->
<?php echo CHtml::textField( 'limite_down', '', array('id'=>'limite') ) ?>
what is TYPE and #limite in this case?
jsebasct
(Jsebas Ct)
April 27, 2011, 2:40pm
7
finally it works !!!
based on this sample: DropDownAjaxDependent
and with the help of Firefox FireBug…
here is the code on my view:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'play_form2',
'enableAjaxValidation'=>false,
)); ?>
<?php
$countries = array(1=>'USA', 2=>'France', 3=>'Japan');
$options = array(
'id' => 'country_id',
'ajax' => array('type'=>'POST'
, 'url'=>CController::createUrl('playTest/udpateTxt')
, 'update'=>'#param_id' //selector to update
)
);
echo CHtml::dropDownList('country_name', '', $countries, $options);
?>
<div id="param_id" class="row">
<?php
echo CHtml::textField( 'temp_id') ;
?>
</div>
<?php $this->endWidget(); ?>
and here is the code on my controller
<?php
class PlayTestController extends Controller {
public function actionUdpateTxt() {
$param_country = (int)$_POST['country_name'];
echo CHtml::tag('input', array( 'type'=>'text' , 'value' => $param_country));
}
}
?>
The part that it is (for me at least) interesting… is that the id that ajax is going to update, has to be a container of other things.
Thats why work when I pass the <div> id, but does not when I pass the ‘temp_id’
(tnks FireBug )
mithila
(Mithila)
April 28, 2011, 10:03am
8
finally it works !!!
based on this sample: DropDownAjaxDependent
and with the help of Firefox FireBug…
here is the code on my view:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'play_form2',
'enableAjaxValidation'=>false,
)); ?>
<?php
$countries = array(1=>'USA', 2=>'France', 3=>'Japan');
$options = array(
'id' => 'country_id',
'ajax' => array('type'=>'POST'
, 'url'=>CController::createUrl('playTest/udpateTxt')
, 'update'=>'#param_id' //selector to update
)
);
echo CHtml::dropDownList('country_name', '', $countries, $options);
?>
<div id="param_id" class="row">
<?php
echo CHtml::textField( 'temp_id') ;
?>
</div>
<?php $this->endWidget(); ?>
and here is the code on my controller
<?php
class PlayTestController extends Controller {
public function actionUdpateTxt() {
$param_country = (int)$_POST['country_name'];
echo CHtml::tag('input', array( 'type'=>'text' , 'value' => $param_country));
}
}
?>
The part that it is (for me at least) interesting… is that the id that ajax is going to update, has to be a container of other things.
Thats why work when I pass the <div> id, but does not when I pass the ‘temp_id’
(tnks FireBug )
hi!! Thanks a lot for putting up your code… it worked for me too:)… butne prob… it doesn get saved in the database. my field stil says it is empty. any clue how??
mithila
(Mithila)
April 28, 2011, 10:28am
9
sorry!! i fixed it… my field id was changing. thanks a lot for your code…
pandiraja
(C Pandit)
February 1, 2013, 6:48am
10
in my _form
<?php
echo '<td><b />Current Week',"</td><td>",$form->dropdownlist($model,'week',CHtml::listData(Timesheet::model()->findAll("status=0 && employee_id=$emp_id"),'id','week_day'),array('style'=>'width: 200px','prompt'=>'Select Week','id' => 'country_id',
'ajax' => array('type'=>'POST',
'url'=>CController::createUrl('timesheetitems/udpateTxt'),
'update'=>'#param_id', //selector to update
'data'=>'js:$(this).serialize()',
))),"</td>";
echo $form->textfield($model,'timesheet_id',array('id'=>'param_id'));
?>
in my controller
public function actionUdpateTxt()
{
$param_country = (int)$_POST['TimesheetItems']['week'];
echo CHtml::tag('input', array( 'type'=>'text' , 'value' => $param_country));
}
but i can’t to get any think in my text field…What is the problem to my code
khanz82
(Anthony)
March 25, 2013, 4:43pm
11
I am having a kind of a simillar issues. What i want to achieve is, i have two dropDownLists and a textfield.
I want to add up the values of the two dropDownLists plus the textfield value and display the result in another textfield.
How would i achieve this.
<tr>
<td><?php echo $form->labelEx($model,'typeofpaper'); ?></td>
<td><?php $data = CHtml::listData(PaperType::model()->findAll(),'id','title');
if(isset($_REQUEST['id']))
{
$sel = $_REQUEST['id'];
} else {
$sel ='';
}
echo $form->dropDownList($model, 'typeofpaper', $data, array('prompt'=>'Type of Paper','options'=>array($sel=>array('selected'=>true))));
?>
<?php echo $form->error($model,'typeofpaper'); ?></td>
</tr>
<tr>
<td><?php echo $form->labelEx($model,'deadline'); ?></td>
<td><?php echo $form->dropDownList($model,'deadline',
array('3hrs'=> '3 hrs', '6hrs'=>'6 hrs', '12hrs'=>'12 hrs', '24hrs'=>'1 day', '48hrs'=>'2 days', '72hrs'=>'3 days',
'96hrs'=>'4 days', '120hrs'=>'5 days', '144hrs'=>'6 days', '168hrs'=>'7 days', '172hrs'=>'8 days', '196hrs'=>'9 days',
'220hrs'=>'10 days', '244hrs'=>'11 days', '268hrs'=>'12 days', '292hrs'=>'13 days', '316hrs'=>'14 days', '340hrs'=>'15 days'),array('empty' => 'Select Urgency Level')); ?>
<?php echo $form->error($model,'deadline'); ?> </td>
</tr>
<tr>
<td><?php echo $form->labelEx($model,'nopages'); ?></td>
<td><?php echo $form->textField($model,'nopages'); ?>
<?php echo $form->error($model,'nopages'); ?></td>
</tr>
k123456
(Kassa 923)
May 23, 2013, 6:37am
12
@jsebas - Thanks for sharing the code, It worked for me!
If the above code doesnt work it might be because you have not defined your action in the accessRules
i.e. :-
In your Controller accessRules()
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update','udpateTxt'),
'users'=>array('@'),
),
NOTE :- updateTxt the action with regard to this example.
Worked for me!
Cheerz!
jigneshsav
(Savaliya Jignesh20)
September 11, 2013, 6:56am
13
finally it works !!!
based on this sample: DropDownAjaxDependent
and with the help of Firefox FireBug…
here is the code on my view:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'play_form2',
'enableAjaxValidation'=>false,
)); ?>
<?php
$countries = array(1=>'USA', 2=>'France', 3=>'Japan');
$options = array(
'id' => 'country_id',
'ajax' => array('type'=>'POST'
, 'url'=>CController::createUrl('playTest/udpateTxt')
, 'update'=>'#param_id' //selector to update
)
);
echo CHtml::dropDownList('country_name', '', $countries, $options);
?>
<div id="param_id" class="row">
<?php
echo CHtml::textField( 'temp_id') ;
?>
</div>
<?php $this->endWidget(); ?>
and here is the code on my controller
<?php
class PlayTestController extends Controller {
public function actionUdpateTxt() {
$param_country = (int)$_POST['country_name'];
echo CHtml::tag('input', array( 'type'=>'text' , 'value' => $param_country));
}
}
?>
The part that it is (for me at least) interesting… is that the id that ajax is going to update, has to be a container of other things.
Thats why work when I pass the <div> id, but does not when I pass the ‘temp_id’
(tnks FireBug )
yes it work for one field, i want this for multiple field update, plz any one help thanks in advance…
hi friend you can use this code in side your script
$(’#brand ’).change(function() {
var id = $('#brand option:selected').val();
$.ajax({
url: "<?php echo Yii::app()->createUrl('controller/getList/id')?>/"+id,
type:'POST',
success: function(res){
$('#mydata2').html(res);
},
error: function(){
alert('failure');
}
});
});
shgninc
(Shgninc)
October 4, 2013, 1:43pm
15
I have the same problem. When i try to change the DropDownList, it take the right value but the jquery represent a error
Toral
(Toralmehta11)
March 14, 2014, 5:59am
16
i m getting same error i can not save value in database and i dont know how to do it…can you help me with this?
just do it:
view
<div class="left15">
<?php
$setores = CHtml::listData(Setor::model()->findAll(),'id','descricao');
$options = array(
'prompt'=>'- Selecione o Setor -',
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('setor/getEmail'), //url to call.
'update'=>'#setor_id', //selector to update
//'data'=>'js:javascript statement'
//leave out the data key to pass all form values through
'success'=>'function(data){$("#Setor_email").attr("value",data);}'
),
);
echo $form->dropDownListRow($model,'setor_id',$setores,$options);
?>
</div>
<div class="left15">
<?php echo $form->labelEx($model->setor,'Email do setor'); ?>
<?php echo CHtml::textField('Setor[email]'); ?>
</div>
controller
public function actionGetEmail()
{
$setor_id = (int)$_POST["Historico"]["setor_id"];
$setor = $this->loadModel($setor_id);
echo $setor->email;
}
Anindya
(Anindyaromauli)
June 29, 2016, 3:34am
18
Hi, I am a newbie. I want to ask you
how to add textfield in dropdownlist? or how to input data use textfied in dropdownlist?