Mohan Prasad
Mohan Prasad

Reputation: 682

Own Ajax Request to delete action in yii2 not working

In my Yii2 I am trying to write my own ajax request to perform delete action.

My code for the action column button **

'delete' => function ($url, $model) {
    return '<span class="glyphicon glyphicon-trash delete-prod" id="'.$model->product_id.'" style="cursor:pointer;"></span>';**                     
}, 

My Ajax request for this :

<?php
if($model!= null){
    foreach($model as $mod);
}
$this->registerJs('
  jQuery("body").on("click", ".delete-prod", function() {
    var id = $(this).attr("id").split("-")[1];
    alert(id);
    $.ajax({
        type: "POST",
        cache: false,
        data:{"refID": id},
        url: "'.Yii::$app->getUrlManager()->createUrl("product/delete?id=".$mod->product_id).'",
        dataType: "json",
        success: function(data){ 
            if(data.status){
                alert("hello");

            }else{
                alert(data.message);
            }
        }
    });
});
');
?>

My Delete action in the controller

public function actionDelete($id)       
{

    \app\models\Productlines::find()->where(['product_id' => $id])->one()->delete();

    return $this->redirect(['index']);
}

For some reason I just get the alert when i click the button. My delete action doesn't work. I get a 500 Internal Server Error. Can any one help me out with this??

Thanks a million...

Upvotes: 0

Views: 2403

Answers (1)

Kiran Muralee
Kiran Muralee

Reputation: 2060

Try this

Your delete button code

'delete' => function ($url, $model) {
    return '<span class="glyphicon glyphicon-trash delete-prod" 
data-id="'.json_encode(["prod_id" => $model->product_id,"area_id" => $model->area_id‌]).'" style="cursor:pointer;"></span>';                     
}, 

Your ajax request code

$url=Yii::$app->getUrlManager()->createUrl("product/delete");
$this->registerJs('
  jQuery("body").on("click", ".delete-prod", function() {

try{
    var model_obj = JSON.parse($(this).attr("data-id"));   
    alert(model_obj.prod_id); //check whether you are getting the correct prod_id value 

    var prod_id = model_obj.prod_id;
    var area_id = model_obj.area_id;

    $.ajax({
        type: "POST",
        cache: false,
        data:{"prod_id":prod_id,"area_id":area_id},
        url: "'.$url.'",
        dataType: "json",
        success: function(data){ 
           alert(data.status);
        }
    });
   }
 catch(e)
 {
    alert(e); //check tosee any errors
 }

});
');

Your Controller code

public function actionDelete()       
{
    Yii::$app->response->format = yii\web\Response::FORMAT_JSON;
    $p = Yii::$app->request->post();

    $prod_id=$p["prod_id"];
    $area_id=$p["area_id"];


    if(\app\models\Productlines::find()->where(['product_id' => $id])->one()->delete())
     {
         return [
            "status" => "success"
        ];
     }
     else
     {
          return [
            "status" => "failure"
        ];
     }


}

Upvotes: 2

Related Questions