Pawan
Pawan

Reputation: 3864

Yii2:redirect to specific action from submit button

I am not able to redirect a custom form to specific action.

What I am trying is

<?= Html::submitButton( 'delete-selected' ,['class' => 'btn btn-primary']) ?>

here delete-selected is my custom action in controller appointment.

I have also tried like this:

public function actionDeleteForm()
    {
        return $this->render('delete');
        return $this->redirect(['delete-selected']);
    }

public function actionDeleteSelected()
{
    Appointment::deleteAll(['doctor_name' =>4]);
    return $this->redirect(['index']);
} 

What I am trying to do is actually delete some records using the form. The form name is delete having a select drop-down field.

I want to post the data to action deleteselected and use the $_POST variable in the delete query.

How can I do this?

Thanks.

Upvotes: 3

Views: 17870

Answers (3)

AceVez
AceVez

Reputation: 301

This is somewhat a note for Joe Miller's answer. If you are supposed to override the form's action with an action of a controller, make sure you make the value of 'action' as an array:

$form = ActiveForm::begin([
'action' => ['appointment/delete-selected']
]);

It will treat the action as a route to action delete-selected in controller appointment.

Upvotes: 1

Joe Miller
Joe Miller

Reputation: 3893

Any submit button that you put on your form will submit to the url specified in the action parameter of the form. If you haven't specified one, then Yii will use the current controller/action of the form. If you want to override this behavior, then you will need to specify an action for the form. e.g.

$form = ActiveForm::begin([
    'action' => 'appointment/delete-selected'
]);

Upvotes: 9

ScaisEdge
ScaisEdge

Reputation: 133400

in actionDeleteForm you have

return $this->render('delete');

before

return $this->redirect(['delete-selected'])

this second instruction will never be executed because you have already made a return to the function and then control has already been returned to the caller

Upvotes: 1

Related Questions