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...