Yii Framework Forum: ajax - update a textField based on a dropdownlist value - Yii Framework Forum

Jump to content

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

ajax - update a textField based on a dropdownlist value Rate Topic: -----

#1 User is offline   jsebas 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 04-April 11

Posted 18 April 2011 - 04:43 PM

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') ) ?>

0

#2 User is offline   twisted1919 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 643
  • Joined: 23-October 10
  • Location:Romania

Posted 18 April 2011 - 05:12 PM

<?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') ) ?>

0

#3 User is offline   twisted1919 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 643
  • Joined: 23-October 10
  • Location:Romania

Posted 18 April 2011 - 05:14 PM

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') ) ?>

0

#4 User is offline   mithila 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 116
  • Joined: 18-April 11

Posted 23 April 2011 - 01:49 AM

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

#5 User is offline   Igor Ivanovic 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 211
  • Joined: 17-October 10
  • Location:Zagreb

Posted 24 April 2011 - 07:35 PM

<?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') ) ?>

0

#6 User is offline   mithila 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 116
  • Joined: 18-April 11

Posted 25 April 2011 - 06:05 AM

View PostIgor Ivanovic, on 24 April 2011 - 07:35 PM, said:

<?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?
0

#7 User is offline   jsebas 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 04-April 11

Posted 27 April 2011 - 09:40 AM

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 :) )
2

#8 User is offline   mithila 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 116
  • Joined: 18-April 11

Posted 28 April 2011 - 05:03 AM

View Postjsebas, on 27 April 2011 - 09:40 AM, said:

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??
0

#9 User is offline   mithila 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 116
  • Joined: 18-April 11

Posted 28 April 2011 - 05:28 AM

sorry!! i fixed it.. my field id was changing. thanks a lot for your code..
0

#10 User is offline   tpandi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 22-May 12

Posted 01 February 2013 - 01:48 AM

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
0

#11 User is offline   wesh 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 08-March 13

Posted 25 March 2013 - 11:43 AM

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> 

0

#12 User is offline   FresherInYII 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 27-February 13
  • Location:Sri-Lanka

Posted 23 May 2013 - 01:37 AM

@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!
0

#13 User is offline   savariya 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 19-February 13
  • Location:india

Posted 11 September 2013 - 01:56 AM

View Postjsebas, on 27 April 2011 - 09:40 AM, said:

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

#14 User is offline   rajesh chaurasia 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 12-January 13
  • Location:Mohali

Posted 03 October 2013 - 11:40 PM

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');
}
});

});
0

#15 User is offline   Shahcheraghean 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 478
  • Joined: 12-December 12
  • Location:Mashhad, IRAN

Posted 04 October 2013 - 08:43 AM

I have the same problem. When i try to change the DropDownList, it take the right value but the jquery represent a error

Quote

[object Object] Not Found- error- Not Found

www.shgn.ir
www.shgn.ir/yii-framework/
yii2.swsco.ir
In a world without any fences or walls,Who needs Gates or Windows!
0

#16 User is offline   Tor_mehta 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 23-January 14

Posted 14 March 2014 - 12:59 AM

View Postmithila, on 28 April 2011 - 05:28 AM, said:

sorry!! i fixed it.. my field id was changing. thanks a lot for your code..



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?
0

#17 User is offline   Bruno Barroso 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 10-January 13

Posted 04 April 2014 - 01:34 PM

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;
	}

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