Hello,
I have already a problem for deleting an item in my gridview.
I have the following function in my controller which will delete the item in the database. I want to call this function per ajax.
public function actionCartdeleteitem()
{
echo "success";
if (isset($_POST['Item']))
{
$sessionid = app()->session->sessionID;
$id = $_POST['id'];
$sql = "DELETE FROM tbcart WHERE sessionid = :sessionid AND id=:id;";
$cmd = app()->getDb()->createCommand($sql);
$cmd->bindParam(':sessionid', $sessionid);
$cmd->bindParam(':id', $id);
$cmd->execute();
}
Yii::app()->end();
}
Here the definition of the column in the gridview
array(
'class' => 'CButtonColumn',
'template' => '{deleteitem}',
'buttons' => array(
'deleteitem' => array(
'label' => 'Delete',
'url' => 'Yii::app()->controller->createUrl("checkout/cartdeleteitem",array("id"=>$data[\'id\']))',
'click' => 'js:function() {deleteitem_onclick( $(this).attr("href"), 2 ); return false;}',
),
),
),
An here the javascript function which is called. But the function will redirect me do another page and I receive never a success from the ajax call. I want to refresh the entire grid on success.
function deleteitem_onclick(url, id) {
var data2post = {
'Item' : {
'id' : id
}
};
$.fn.yiiGridView.update('cart-grid', {
type :'POST',
url : url,
data : data2post,
success:function(data,status) {
$.fn.yiiGridView.update('cart-grid');
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.responseText + textStatus + errorThrown);
}
});
}
/*]]>*/
What is wrong with my code?