ajax - update a textField based on a dropdownlist value

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






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



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



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…




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

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

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

in my _form

<?php

	echo '&lt;td&gt;&lt;b /&gt;Current Week',&quot;&lt;/td&gt;&lt;td&gt;&quot;,&#036;form-&gt;dropdownlist(&#036;model,'week',CHtml::listData(Timesheet::model()-&gt;findAll(&quot;status=0 &amp;&amp; employee_id=&#036;emp_id&quot;),'id','week_day'),array('style'=&gt;'width: 200px','prompt'=&gt;'Select Week','id' =&gt; 'country_id',


			'ajax' =&gt; array('type'=&gt;'POST',


                 			'url'=&gt;CController::createUrl('timesheetitems/udpateTxt'),


                 			'update'=&gt;'#param_id',  //selector to update


							'data'=&gt;'js:&#036;(this).serialize()',


        	))),&quot;&lt;/td&gt;&quot;; 


					


	echo &#036;form-&gt;textfield(&#036;model,'timesheet_id',array('id'=&gt;'param_id'));	

?>

in my controller

public function actionUdpateTxt()

{


	&#036;param_country = (int)&#036;_POST['TimesheetItems']['week'];


	echo CHtml::tag('input', array( 'type'=&gt;'text' , 'value' =&gt; &#036;param_country));


}

but i can’t to get any think in my text field…What is the problem to my code

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> 



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

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 = &#036;('#brand option:selected').val();


 &#036;.ajax({


	 url: &quot;&lt;?php echo Yii::app()-&gt;createUrl('controller/getList/id')?&gt;/&quot;+id,


	 type:'POST',


	 success: function(res){


		   &#036;('#mydata2').html(res);


	  },


	  error: function(){


	    alert('failure');


	  }


	 });


	


 });

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

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;

	}



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?