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

    <?php echo $form->labelEx( ScriptQuestion::model(),'Field'); ?></td><td>
    <?php echo CHtml::activedropDownList( ScriptQuestion::model(),'crm_base_contact_form_field_id',$select_field,       
                '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();
                            } ',                    
    ); ?>               
        <?php echo $form->textArea($model, 'message', array('id'=>'question_editor','maxlength'=>508, )); ?>

This is controller action:

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



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

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

   var selected = $("#send_bcfield option:selected").val();

Or change your code to this :

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

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




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


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.