Hi,
I have an Ajax function that adds a row to my database, after that, I would like to repopulate a dropdownlist. I guess I can empty the div that contains my dropdown and add a new updated one. But can I do this from the controller or do I need to use jQuery to do this?
view:
<?php
//a dropdown list populated by some data from my database
echo CHtml::dropDownList('album', '', CHtml::listData($ddl, 'idalbum', 'naziv'), array(
'ajax' => array(
'type' => 'POST', //request type
'url' => CController::createUrl('slika/dolociAlbumUpload'), //url to call.
'data' => array('album' => 'js:this.value'),
),
'class' => 'ddl',
'id' => 'album'));
?>
<script>
//ajax for adding a new variable to the database
$('#dodajnalbum').click(function() {
var input = $('#nalbum').val();
if (input != "") {
$.ajax({
type: 'POST',
url: '/imago/slika/dodajnovialbum', //ali $('#user-form').attr('action')
data: ({nalbum : input}),
success: function(msg) {
alert(msg);
}
});
}
});
</script>
controller action:
<?php
public function actionDodajNoviAlbum(){
if (Yii::app()->request->isAjaxRequest){
$novialbum = $_POST['nalbum'];
$model = new Album;
$model->attributes = array('naziv' => $novialbum, 'uporabnik_iduporabnik' => intval(Yii::app()->user->id), 'zasebno' => 0);
if($model->save())
//if the new value is saved, then I would like to update my dropdownlist without refreshing my page
}
}
?>