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

29

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

Answer

Solution:

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.

389

Answer

Solution:

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

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

People are also looking for solutions to the problem: javascript - Remove button in table row

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.