Update Textfield With Value After Dropdownlist Is Selected

Hi, I need a help here. Currently, I am having 1 dropdownlist, 1 dependent dropdownlist and 1 textfield for my submitted form. Both of the dropdownlist are working perfectly but not for the textfield.

I wish to have a value return on the textfield after I have selected the dependent dropdownlist. Any idea how to solve this problem? Any help is appreciated. Please help me~ Thank you.

Here’s my code:

view file




//dropdownlist

<div style="margin-left:70px">

    <?php echo $form->labelEx($model,'code'); ?>

	<div style="margin-left:110px; margin-top:-30px">

	<?php echo $form->dropDownList($model,'code', CHtml::listData(Course::model()->findAll(), 'code', 'code'), array(

	'empty'=>'Please Select Course Code',

	'ajax'=> array(

	         'type'=>'Post',

			 'url'=>CController::createUrl('Exam/getcode2'),

			 'update'=>'#'.CHtml::activeId($model,'code2'),

			 'data'=>array('code'=>'js:this.value'), 

	))); ?>

    </div></div></br>


//dependent dropdownlist

	<div style="margin-left:70px">

    <?php echo $form->labelEx($model,'code2'); ?>

	<div style="margin-left:110px; margin-top:-30px">

	<?php echo $form->dropDownList($model,'code2', array(),array(

	'empty'=>'Please Select Course Code',

	'ajax'=> array( 

	         'type'=>'Post',

			 'url'=>CController::createUrl('Exam/getname'),

			 'update'=>'#'.CHtml::activeId($model,'name'),

			 'data'=>array('code2'=>'js:this.value'), 

	))); ?>

	

    </div></div></br>


//textfield	

	<div style="margin-left:70px">

    <?php echo $form->labelEx($model,'name'); ?>

	<div style="margin-left:110px; margin-top:-30px">

	<?php echo $form->textfield($model,'name', array(''));?>

	</div></div></br>



controller file




public function actionGetcode2()

    {

        $data=Course::model()->findAll('code=:code', 

                  array(':code'=> $_POST['code']));


        $data=CHtml::listData($data,'code2','code2');

            foreach($data as $value=>$code2)  

			{

               echo CHtml::tag('option', array('value'=>$value),CHtml::encode($code2),true);

            }   

    }

	

	public function actionGetname()

	{     

	   //how to populate the value for textfield after select the dependent dropdownlist

    } 



write some java script luke this

jQuery(’#dropDownId’).change(

function(){

$(’#textboxId’).val(jQuery(this).val());

}

);

Hi, Ahamed Rifaideen. Thank you so much for your reply. I have changed the code like what you have suggested but I got a syntax error [color="#FF0000"]‘Parse error: syntax error, unexpected ‘(’, expecting variable (T_VARIABLE) or ‘$’ in C:\xampp\htdocs\fyp\protected\views\exam\_form.php on line 50’.[/color]

I tried to solve it but fail. Any idea?? Thanks.




<div style="margin-left:70px">

    <?php echo $form->labelEx($model,'code'); ?>

	<div style="margin-left:110px; margin-top:-30px">

	<?php echo $form->dropDownList($model,'code', CHtml::listData(Course::model()->findAll(), 'code', 'code'), array(

	'empty'=>'Please Select Course Code',

	'ajax'=> array(

	         'type'=>'Post',

			 'url'=>CController::createUrl('Exam/getcode2'),

			 'update'=>'#'.CHtml::activeId($model,'code2'),

			 'data'=>array('code'=>'js:this.value'), 

	))); ?>

    </div></div></br>


	<div style="margin-left:70px">

    <?php echo $form->labelEx($model,'code2'); ?>

	<div style="margin-left:110px; margin-top:-30px">

	<?php echo $form->dropDownList($model,'code2', array(

	'empty'=>'Please Select Course Code',

	'id'=>'code2',

	// 'ajax'=> array( 

	         // 'type'=>'Post',

			 // 'url'=>CController::createUrl('Exam/getname'),

			 // 'update'=>'#'.CHtml::activeId($model,'name'),

			 // 'data'=>array('code2'=>'js:this.value'), )

    )); ?>

    </div></div></br>

	

	<?php

	jQuery("#code2").change( 

    function() 

	{

     $("#name").val(jQuery(this).val());

    });

     ?>

	

	<div style="margin-left:70px">

    <?php echo $form->labelEx($model,'name'); ?>

	<div style="margin-left:110px; margin-top:-30px">

	<?php echo $form->textfield($model,'name', array(

	'id'=>'name',

	));?>

	</div></div></br>



its not a php it is a java script jquery snippet so enclose the script with <script> tag not php

Sorry for my stupid mistakes. Never realized that until you mentioned it. Thanks again.

Thanks for guiding me! :D

Hi, Ahamed Rifaideen. Can I ask you another question? I am doing append data for <div> by using javascript now. But, I am struggling on how to insert data for the field that I have appended? Can u guide me on this?

I have a button to click and once I click, the content of the <div> will be appended. How am I going to insert the data that are filled into the appended field? Thanks in advance~

Here’s my code:




<script>

	var _counter = 0;

    function Add() {

    _counter++;

    var oClone = document.getElementById("template").cloneNode(true);

    oClone.id += (_counter + "");

    document.getElementById("add").appendChild(oClone);

    }</script>


	<div id="add">

	<div id="template">

	<fieldset style="margin-left:10px; margin-right:10px; background-color:#FFFFE0">

	<div style="margin-left:85px">

    <?php echo $form->labelEx($model,'day'); ?>

	<div style="margin-left:95px; margin-top:-30px">

	<?php echo $form->dropDownList($model,'day', CHtml::listData(CourseType::model()->findAll(), 'day', 'day'), array('empty'=>'Please Select Day')); ?>

    </div></div></br>


	<div style="margin-left:48px">

    <?php echo $form->labelEx($model,'start'); ?>

	<div style="margin-left:130px; margin-top:-30px">

	<?php 

    $this->widget('ext.timepicker.timepicker', array(

    'model'=>$model,

    'name'=>'start',

    ));

    ?> </div></div></br>


	<div style="margin-left:48px">

    <?php echo $form->labelEx($model,'end'); ?>

	<div style="margin-left:130px; margin-top:-30px">

	<?php 

    $this->widget('ext.timepicker.timepicker', array(

    'model'=>$model,

    'name'=>'end',

    ));

    ?> </div></div>


    </div></div>   

	

	<div style="margin-left:20px">

	<?php echo CHtml::button('Add More Details', array('onclick' => 'Add()')); ?>

	</div>



am not a master in js. use jQuery append() method.

am not a master in js. use jQuery append() method.

Hi, Ahamed Rifaideen. I have a question here. I can update the textfield successfully after a dropdownlist is selected. But, I can’t save the correct value into the database.

Here’s my code:




<div style="margin-left:70px">

    <?php echo $form->labelEx($model,'code'); ?>

	<div style="margin-left:110px; margin-top:-30px">

	<?php echo $form->dropDownList($model,'code', CHtml::listData(Course::model()->findAll(), 'name', 'code'), array(

	'empty'=>'Please Select Course Code',

	'id'=>'code',

	)); ?>

    </div></div></br>

	

	<script type="text/javascript">

	jQuery("#code").change( 

    function() 

	{

     $("#name").val(jQuery(this).val());

    });

    </script>

	

	<div style="margin-left:70px">

    <?php echo $form->labelEx($model,'name'); ?>

	<div style="margin-left:110px; margin-top:-30px">

	<?php echo $form->textfield($model,'name', array(

	'id'=>'name',

	));?>

	</div></div></br>



I wish to save the ‘code’ for the dropdownlist into database. If I changed the ‘name’ into ‘code’ then it will return code on the textfield. If I don’t change, it returned correctly on the textfield but it doesn’t save ‘code’ into the database. It saves ‘name’ into the database.

Anyone knows how to solve this?? Thank you.