Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YII: How to update ajax data on page load?

I have a dropdownlist which updates an ajax field. All works fine but the ajax field does not get filled with data on page load. How can I force the ajax field update, on page load?

This is the dropdownlist in view:

echo CHtml::dropDownList('category_selection_code', '', $cat_list_data, 
        array('style'=>'width:400px',
              'ajax' => 
                        array(
                            'type'=>'POST', 
                            'url'=>CController::createUrl('articles/GetContentFields'), 
                            'update'=>'#ExtraContentFields', 

                        )
            )
);

*UPDATE

I've found the solution which is working for me:

$(document).ready(function(){
    $.ajax({
        type: 'POST',
        data: {category_selection_code: $('#category_selection_code').val()},
        url: '<?php echo CController::createUrl('articles/GetContentFields'); ?>',
        success: function(data){
                    $('#ExtraContentFields').html(data)
                }
    })
})

Controller for ajax processing articles/GetContentFields is waiting category_selection_code parameter in $_POST array. And we should to set it in the data section of ajax part:

data: {category_selection_code: $('#category_selection_code').val()},


Thnx all for the help.

like image 921
Mykola Makhynko Avatar asked Nov 22 '25 14:11

Mykola Makhynko


2 Answers

I don't think what you've done above is correct. Firstly, the fourth parameter to CHtml::dropdownList() function is an HTML options array. See the function signature for CHtml::dropdownList()

Put your AJAX call within

$(document).ready(function() {
 //Your call goes in here. 
});
like image 133
verisimilitude Avatar answered Nov 25 '25 03:11

verisimilitude


Fire change event worked for me. Also, I found useful CHtml::activeId(..) method.

<?php
$cs = Yii::app()->clientScript->registerScript(
  'update-brand-car-on-page-load',
  '$(document).ready(function() {
        $("#'.CHtml::activeId($model, 't_brand_car').'").change();
    });'
);
?>
like image 23
Ilya Avatar answered Nov 25 '25 02:11

Ilya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!