Pawan Pawan - 1 year ago 808
PHP Question

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']) ?>

is my custom action in controller

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
having a select drop-down field.

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

How can I do this?


Answer Source

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'