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 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 517
  • 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 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 517
  • 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: 208
  • 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 :) )
0

#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 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • 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

Share this topic:


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

3 User(s) are reading this topic
0 members, 3 guests, 0 anonymous users