0

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

12
  • what error do you get in firebug?check the reason for internal server error Commented Sep 16, 2016 at 9:12
  • I get Internal Server Error: ** POST test.qsims.com/index.php/product/delete?id=4 500 (Internal Server Error)** Commented Sep 16, 2016 at 9:14
  • where is your button that you click on it for delete? Commented Sep 16, 2016 at 9:17
  • the code for action column button.. I have given a class you can see there Commented Sep 16, 2016 at 9:24
  • 1
    delete ?id=".$mod->product_id from your url and in your action get id by $_POST['refID']; Commented Sep 16, 2016 at 9:41

1 Answer 1

2

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"
        ];
     }


}
Sign up to request clarification or add additional context in comments.

8 Comments

if I want to send more than 1 data to the controller how do i send it?
data:{"refID": id ,"anotherData":"testdata"}
I mean to say I want use my $model and send it... So like for example I have passed id = $model->product_id. The same way I want to pass $model->area_id to the controller
@Mohan Prasad my suggestion is that u change code like return '<span class="glyphicon glyphicon-trash delete-prod" data-id="{'prod_id':".$model->product_id.",'area_id':".$model->area_id."}" style="cursor:pointer;"></span>';** .Then get this data-id attribute value in javascript as JSON.parse($(this).attr("data-id"));Learn more about JSON in w3schools.com/json/json_intro.asp
can you look at your new answer I have made edits It gets an error : Uncaught SyntaxError: Unexpected token p in JSON at position 1
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.