Bala
Bala

Reputation: 85

How to Update textfield onchange of dropdownlist without refresh page in Yii?

I have a Yii dropdownlist and text field, when i select the dropdown list item, this name should be displayed in textfield. I tried this concept using ajax with But it updates after page refresh only.I have pasted my code here, please look-through and suggest me how to set textfield after every immediate selection of drop down listed item.

   The following code resides protected/views/form

    <td>    
    <?php echo $form->labelEx( ScriptQuestion::model(),'Field'); ?></td><td>
    <?php echo CHtml::activedropDownList( ScriptQuestion::model(),'crm_base_contact_form_field_id',$select_field,       
                array(
                'id' => 'send_bcfield',
                'class' => 'col_165',
                'ajax' => array(
                    'type' => 'POST',
                    'url' => CController::createUrl('DisplayMessage'),                                                               
                    'update' => '#question_editor',
                    'data' => array('bcfield' => 'js:this.value'),
                    'success'=> 'function(data) {$("#question_editor").empty();
                            $("#question_editor").val(data);
                            } ',                    
                    ))      
    ); ?>               
    </td>
         <td>
        <?php echo $form->textArea($model, 'message', array('id'=>'question_editor','maxlength'=>508, )); ?>
           </td>

This is controller action:

    public function actionDisplayMessage(){ 
$q = $_POST['bcfield'];
$model=ScriptQuestion::model()->findAll();  
$sql = "SELECT name  FROM crm_field WHERE crm_field_id=". $q ;
    $command = Yii::app()->db->createCommand($sql);
    $result= $command->queryScalar(); 
    echo "%".$result."%";
    $this->performAjaxValidation($model);       
    }

Upvotes: 3

Views: 9711

Answers (2)

Alireza Fallah
Alireza Fallah

Reputation: 4607

No need to Ajax, it's just a simple javascript/jQuery .

Just do this ( replace editor1 with your ckeditor instance name) :

<script>
$("#send_bcfield").change(function(){
   var selected = $("#send_bcfield option:selected").val();
   CKEDITOR.instances.editor1.setData(selected);
});
</script>

Or change your code to this :

<?php
echo CHtml::activedropDownList(ScriptQuestion::model(), 'crm_base_contact_form_field_id', $select_field, array(
    'id' => 'send_bcfield',
    'class' => 'col_165',
    'onchange' => '$("#send_bcfield").change(function(){
                    var selected = $("#send_bcfield option:selected").val();
                    CKEDITOR.instances.editor1.setData(selected);
                    });',
    )
);
?>

Update : I changed my code to changing a ckeditor value according to your comment below my answer.

Upvotes: 2

sakhunzai
sakhunzai

Reputation: 14470

probably you simply need to add this to CHtml::activedropDownList settings:

  'onchange' => "$('question_editor').val($('#send_bcfield option:selected').text())"

Upvotes: 1

Related Questions