How To Create A Custom Button That Will Delete A Record

Hello everyone. I am displaying the products records in index page in which I want to display a delete button with each Item, so that when click on that delete button then it should delete that Item record.

I have set something but doesn’t work,




<?php

                    echo CHtml::button('Delete', array(

                        'submit' => array('item/delete', array('id' => $data->id)),

                        'confirm' => 'Are you sure?'

                            )

                    );

                    ?>



and here is the delete action


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.');

	}

Where I am doing mistake ??

Thanks in advance.

echo CHtml::button(‘Delete’, array(

                    'submit' =&gt; array('item/delete', 'id' =&gt; &#036;data-&gt;id),


                    'confirm' =&gt; 'Are you sure?'


                        )


                );

I don’t see that U configured any response in delete action. Also U are not clear enough about “I have set something but doesn’t work”. Are U getting any errors, or blank page or what ever

1 Like

when I click on the button then it ask for confirmation but when press "yes" then it does not delete that record, and stay on that page.

I am confused that where I am doing mistake.