Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
soopers

How To Create A Custom Button That Will Delete A Record

Recommended Posts

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. 

Share this post


Link to post
Share on other sites

If I'm following your code correctly, your button makes a GET request, submitting the browser to the page for deleting. But your code requires a POST request. I'd recommend having this done via Ajax, the way the grid view does it.

  • Upvote 1

Share this post


Link to post
Share on other sites

Thanks sir for your response. 

Sir I am new to yii and also have no idea of ajax. 

So if you write the code which you have told, then it will be effecient for me.

 

Thanks

Share this post


Link to post
Share on other sites

Well, I'm not going to write the code for you. You can either change what you have to allow for a GET response, or you could change the button to a form that makes a POST request, or you can look into the admin grid view code to see how that works, or you could search online.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...