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.
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.
});
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();
});'
);
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With