Yii Framework Forum: Dependent Textfield After Dropdownlist Is Selected - Yii Framework Forum

Jump to content

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

Dependent Textfield After Dropdownlist Is Selected Rate Topic: -----

#1 User is offline   beginner123 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 84
  • Joined: 03-March 14

Posted 25 April 2014 - 02:33 AM

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

#2 User is offline   Absar A Hasan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 28
  • Joined: 23-November 13
  • Location:Pune, India

Posted 25 April 2014 - 04:14 AM

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

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

}


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

#3 User is offline   beginner123 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 84
  • Joined: 03-March 14

Posted 25 April 2014 - 10:42 AM

View PostAbsar A Hasan, on 25 April 2014 - 04:14 AM, said:

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

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

}


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

#4 User is offline   beginner123 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 84
  • Joined: 03-March 14

Posted 26 April 2014 - 01:07 AM

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

0

#5 User is offline   jkofsky 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 530
  • Joined: 17-May 10
  • Location:Pensacola, Florida

Posted 26 April 2014 - 01:56 AM

View Postbeginner123, on 26 April 2014 - 01:07 AM, said:

<script type="text/javascript">
    function Getname()
...
            'success'=>"function(data)
            {
                $('#name').val(data.name);
            } ",
...
</script>

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



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.
Do, or do not. There is no 'try.' Jedi Master Yoda
0

#6 User is offline   beginner123 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 84
  • Joined: 03-March 14

Posted 26 April 2014 - 02:24 AM

View Postjkofsky, on 26 April 2014 - 01:56 AM, said:

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>

0

#7 User is offline   beginner123 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 84
  • Joined: 03-March 14

Posted 26 April 2014 - 09:39 AM

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>

0

#8 User is offline   jkofsky 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 530
  • Joined: 17-May 10
  • Location:Pensacola, Florida

Posted 26 April 2014 - 12:38 PM

View Postbeginner123, on 26 April 2014 - 09:39 AM, said:

<script type="text/javascript">
	<?php echo $form->dropDownList($model,'code', CHtml::listData(Course::model()->findAll(), [color="#FF0000"]'code', 'name'[/color]), array(


I think 'name', 'code' is backward to get the code returned.
Do, or do not. There is no 'try.' Jedi Master Yoda
0

#9 User is offline   beginner123 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 84
  • Joined: 03-March 14

Posted 27 April 2014 - 02:02 AM

View Postjkofsky, on 26 April 2014 - 12:38 PM, said:

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

#10 User is offline   jkofsky 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 530
  • Joined: 17-May 10
  • Location:Pensacola, Florida

Posted 27 April 2014 - 09:55 PM

View Postbeginner123, on 26 April 2014 - 09:39 AM, said:

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?

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.
Do, or do not. There is no 'try.' Jedi Master Yoda
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