Yii2 form submit button must be clicked two times for action. How to prevent this?

巧了我就是萌 提交于 2019-12-10 20:48:33

问题


yii2 submit button needs to be clicked two times in form

I have a problem where I need to check more than one submit buttons in the controller. It works but I need to click submit buttons two times. In controller :

switch(\Yii::$app->request->post('submit')) {
                case 'submit_1' :

                  //my code 
                   break;  

                case 'submit_2' :                   
                 //my code 

In view

<?= Html::submitButton('NEXT', ['name' => 'submit', 'value' => 'submit_2','class'=>'btn btn-primary pull-right']) ?>
<?= Html::submitButton('PREVIOUS', ['name' => 'submit', 'value' => 'submit_3','id'=>'next_summary', 'class' => 'btn btn-primary pull-right']) ?>

回答1:


There is an issue with using jquery reserved words as your attribute id or attribute names.

Search for "Be careful when naming form elements such as submit buttons" at https://github.com/yiisoft/yii2/blob/master/docs/guide/input-forms.md

Search "Additional Notes" at https://api.jquery.com/submit/

Changing your submit names will fix your click twice problem:

<?= Html::submitButton('NEXT', ['name' => 'submit_next', 'value' => 'submit_2','class'=>'btn btn-primary pull-right']) ?>
<?= Html::submitButton('PREVIOUS', ['name' => 'submit_prev', 'value' => 'submit_3','id'=>'next_summary', 'class' => 'btn btn-primary pull-right']) ?>



回答2:


try to change name of button as array

 <?= Html::submitButton('NEXT', ['name' => 'submit[]', 'value' => 'submit_2','class'=>'btn btn-primary pull-right']) ?>
<?= Html::submitButton('PREVIOUS', ['name' => 'submit[]', 'value' => 'submit_3','id'=>'next_summary', 'class' => 'btn btn-primary pull-right']) ?>

and in your controller :

$submittedType = \Yii::$app->request->post('submit');
switch($submittedType[0]) {
   //your code
}


来源:https://stackoverflow.com/questions/38557838/yii2-form-submit-button-must-be-clicked-two-times-for-action-how-to-prevent-thi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!