Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii Error 400 The CSRF token could not be verified when trying to delete the post

Tags:

php

yii

When I was trying to delete a post I got this error:

Yii Error 400 The CSRF token could not be verified

I don't know what is exactly causing this and to what it could be related. here is my action delete:

    public function actionDelete($id) {

         if (Yii::app()->request->isPostRequest) {
                // we only allow deletion via POST request
                $this->loadModel($id)->delete();

                // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
                if (!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
        }
        else
              throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
    }

    protected function afterDelete()
    {
        parent::afterDelete();
        Image::model()->deleteAll('name='.$this->id);
        Date::model()->deleteAll('tbl_show_id='.$this->id);
        Press::model()->deleteAll('tbl_show_id='.$this->id);
    }
like image 592
mahsa.teimourikia Avatar asked Jun 22 '12 08:06

mahsa.teimourikia


1 Answers

I had the same issue but the following solved it. Hope it helps. I added

'params'=> array('YII_CSRF_TOKEN' => Yii::app()->request->csrfToken)

to the following code:

<?php
    echo CHtml::linkButton('Delete',array(
        'submit'=>$this->createUrl('delete',array('id'=>$model->id)),
        'confirm'=>"Are you sure want to delete ".$item->product->name."from the shopping cart?",
        'params'=> array('YII_CSRF_TOKEN' => Yii::app()->request->csrfToken)));
?>

Thanks.

like image 66
bonnie Avatar answered Oct 03 '22 22:10

bonnie