Dependent Textfield After Dropdownlist Is Selected

Hi, I am facing a problem of updating the textfield with value after a value of dropdownlist is selected. When I select a value from dropdownlist, the dependent textfield is not shown any value out. Anyone knows how to solve this?




<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',

	'id'=>'code',

	'ajax'=> array(

	         'type'=>'Post',

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

			 'update'=>'#name',

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

	))); ?>

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

	

	

//dependent textfield

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

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

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

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

	</div></div>






public function actionGetname()

   {

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

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


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

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

			{

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

            }   

   }



I followed the link here:link but it does not works to me. Is there anything I miss out here? Please help me…

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

'empty'=&gt;'Please Select Course Code',


'id'=&gt;'code',


'ajax'=&gt; array(


         'type'=&gt;'Post',


		 'url'=&gt;CController::createUrl('Exam/getname'),


		[color=&quot;#FF0000&quot;] 'update'=&gt;'#name',[/color]


		 'data'=&gt;array('code'=&gt;'js:this.value'), 


))); ?&gt;

public function actionGetname()

{

    &#036;data=Course::model()-&gt;findAll('code=:code', 


              array(':code'=&gt; &#036;_POST['code']));





    &#036;data=CHtml::listData(&#036;data,'name','name');


        [color=&quot;#FF0000&quot;]foreach(&#036;data as &#036;value=&gt;&#036;name)  


		{


           echo CHtml::tag('option', array('value'=&gt;&#036;value),CHtml::encode(&#036;name),true);


        } [/color]  

}

First, your are updating div with id="name" instead of updating input field value and then in ajax url action you outputting option tag instead input field value.

See red marked…

Hi,Absar A Hasan. Thanks for the reply. Ohh, that’s my mistake. Do you have any idea on how to solve it? How to make it to update the textfield? Please guide me…

I have tried to change into another method of getting value for textfield after dropdownlist is selected but it is still not working. Could anyone please help me?? I followed the link HERE




<script type="text/javascript">

    function Getname()

    {

        <?php

        echo CHtml::ajax(array(

            'url'=>CController::createUrl('Getname'),

            'data'=>array('code'=>'js:$(\'#code\').val()'),

            'type'=>'post',

            'dataType'=>'json',

            'success'=>"function(data)

            {

                $('#name').val(data.name);

            } ",

            ));?>

      // alert(data);

        return false;

    }

</script>


	

	

	<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','onchange'=>'{Getname();}')); ?>

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

	

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

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

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

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

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






public function actionGetname()

   {

         if(Yii::app()->request->isAjaxRequest)

            {

                $id=$_POST['code'];

                if($id != '')

                {

                    $data=Course::model()->findAll($id);

                    echo CJSON::encode(array(

                        'name'=>$data['name']

                    ));

                    Yii::app()->end();

                }

             } 

   }



I think #name won’t exist on your form. Look at the displayed source code in the browser. I think the actual id of the this $form->textfield() is ‘modelName_name’. So (#name).val() has nothing to point to.

Hi,jkofsky! You are totally right. The id of #name is not exist. Thanks for correcting me but it is still not working after I have changed the ID. Is there anything I miss out again? THe value of textfield is not shown after I select dropdownlist.




<script type="text/javascript">

    function Getname()

    {

        <?php

        echo CHtml::ajax(array(

            'url'=>CController::createUrl('Getname'),

            'data'=>array('code'=>'js:$(\'Exam_code\').val()'),

            'type'=>'post',

            'dataType'=>'json',

            'success'=>"function(data)

            {

                $('#Exam_name').val(data.name);

            } ",

            ));?>

      // alert(data);

        return false;

    }

</script>


	

	

	<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','id'=>'Exam_code','onchange'=>'{Getname();}')); ?>

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

	

	<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'=>'Exam_name','disabled'=>true));?>

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



I have changed my code and it works fine to me. However, I wish to save ‘code’ instead of ‘name’ into my database. How to solve this problem?




<script type="text/javascript">

    function change()

	{

	var select = document.getElementById('code');

	var input = document.getElementById('name');

	

		input.value = select.value;

		

	}

    </script>

	

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

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

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


//i wish to save the 'code' instead of 'name'. if i change the 'name' to 'code', then the 'name' will not be displayed on the textfield below


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

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

	'id'=>'code',

	'onchange'=>'{change();}'

	)); ?>

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

	

	<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','disabled'=>true));?>

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



I think ‘name’, ‘code’ is backward to get the code returned.

If I change ‘name’,‘code’, then it will save name for the both dropdownlist and textfield into the database.

If I change ‘code’,‘name’, then it will save code for the both dropdownlist and textfield into the database.

How am I going to solve the problem if I want to save code for dropdownlist and name for textfield into the database?? Hope you can understand what I am trying to explain here. Thank you.

That’s the question I was answering.


<script type="text/javascript">

    function change()

        {

        var select = document.getElementById('code');

        var input = document.getElementById('name');

        

                input.value = select.value;

                

        }

    </script>



Save the value from select with code, name datalist, then change above to


input.value = select.text

I think. I think you need to set the input.value to the selection text.