sush
sush

Reputation: 379

YII : Ajax database update in ClistView

I want to update a record in my database based on a click in Yii. I have tried various methods but the request is not being posted by the button.

Here is my ajax button Code

CHtml::ajaxButton('Done',Yii::app()->createUrl('task/updateTask'),
                    array(
                        'type'=>'POST',
                        'data'=> array('id'=>$data->task_id),                        
                        'success'=>'js:function(string){ alert(string); }',

                    ),array('class'=>'btn btn-success',)); 

The request is sent via POST method to the Controller and the code for controller is

public function actionupdateTask(){                    
            $query = "UPDATE task SET task_status=4 WHERE task_id=:t_id";
                $command = Yii::app()->db->createCommand($query);
                $command->bindValue(':t_id', $_POST['id'], PDO::PARAM_INT );
                $command->execute();
                 Yii::app()->end();

        }

But the button is not working or sending data. If i check the source of the page it shows

jQuery('#yw0').yiiListView({'ajaxUpdate':['yw0'],'ajaxVar':'ajax','pagerClass':'pagination','loadingClass':'list-view-loading','sorterClass':'sorter','enableHistory':false});
jQuery('body').on('click','#yt0',function(){jQuery.ajax({'type':'POST','data':{'id':'5'},'success':function(string){ alert(string); },'url':'/tasks_yii/index.php?r=task/updateTask','cache':false});return false;});
jQuery('body').on('click','#yt1',function(){jQuery.ajax({'type':'POST','data':{'id':'4'},'success':function(string){ alert(string); },'url':'/tasks_yii/index.php?r=task/updateTask','cache':false});return false;});
jQuery('body').on('click','#yt2',function(){jQuery.ajax({'type':'POST','data':{'id':'1'},'success':function(string){ alert(string); },'url':'/tasks_yii/index.php?r=task/updateTask','cache':false});return false;})

it seems ok but it is not working.

Upvotes: 2

Views: 261

Answers (2)

sush
sush

Reputation: 379

Well the problem has now been solved, it was due to the non closure of script tag which blocked the other scripts and which in turn prevented all the ajax calls made

Upvotes: 0

Alex
Alex

Reputation: 8072

You shuld replace actionupdateTask with actionUpdateTask

public function actionUpdateTask(){                    
    $query = "UPDATE task SET task_status=4 WHERE task_id=:t_id";
    $command = Yii::app()->db->createCommand($query);
    $command->bindValue(':t_id', $_POST['id'], PDO::PARAM_INT );
    $command->execute();
    Yii::app()->end();
}

Upvotes: 1

Related Questions