Submit form on delete

I’ve added this delete button inside an update form:


                                    

echo Html::a('<i class="entypo-trash"></i>' . Yii::t('app', 'Delete'), ['delete', 'id' => $model->id], [

    'class' => 'btn btn-danger btn-icon',

    'data' => [

        'confirm' => Yii::t('app', 'You are about to permanently delete this phone call (the phone call history will disappear). Are you sure you want to delete it?'),

        'method' => 'post',

    ],

]);

When I press the link, confirm dialog appears (ok) but if I press OK button in this dialog, delete action is not executed, just a form submit.

Is something wrong in my code?

If I allow get delete action in controller and I change the anchor to work with get everything works… I don’t know if this can help. Perhaps just 1 “post anchor”/“submit button” is allowed in each form?

Using an submit element will submit form data - this will refresh your page.

You have to use a button element which is calling an "onClick"-function. with this you can post your data from js without reloading page (jQuery´s $.ajax-function / $.post-function as example).

To fetch your post data you can use also following code:





$("#myDeleteButton").click(function() {

    $.post(url, $("#myForm").serialize(), function(data) {

        // this function is called after post request, so it means after delete action

        // $.serialize() creates a json object from your form containing input data

    });

});




I’ll try it, but I’m not using a submit element. Just an anchor no?

As i said, using submit will refresh. If you dont use one, then it should not reload expect using a link.

Using a simple element with onClick should do what you want. After your action (or with it) you can force reload data:


$("#myElement").load($url, $data);