简体   繁体   English

PHP:处理多个提交按钮

[英]PHP: Handling Multiple Submit Buttons

Would like to get a consensus as to what the best practice is in this scenario: 希望就这种情况下的最佳实践达成共识:

Muliple submit buttons, is it better to handle this by having separate FORMS for each one of the submits, OR is it okay to have one form and check which button was pressed? 多个提交按钮,是否最好通过为每个提交都有单独的FORMS来处理此问题,还是可以使用一种表单并检查按下了哪个按钮?

thank you for your input :D 谢谢您的投入:D

Assuming you're doing this solely in PHP, just change the values for each button and check for existence in POST: 假设您仅在PHP中执行此操作,只需更改每个按钮的值并检查POST中是否存在:

if (isset($_POST['button1']) && $_POST['button1'] == "Previous") {

  // do something

} else if (isset($_POST['button1']) && $_POST['button1'] == "Next") {

  // do something else

} else {

  // nothing has been submitted, display default

}

In my case I would do that similar to Andrew Heath, but I woulg give the buttons a unique name. 就我而言,我会像安德鲁·希思(Andrew Heath)那样做,但是我会给按钮起一个唯一的名字。 So the && clause is not needed 因此不需要&&子句

Well, depending on the content of the form, having 2 submit buttons would be the only way to achieve what you want. 好吧,根据表单的内容,拥有2个提交按钮将是实现您想要的唯一方法。 (How would you have 2 forms if there were text boxes.. replicating the entered data with javascript would be rather annoying) (如果有文本框,您将有2种形式。用javascript复制输入的数据会很烦人)

I would stick with just checking to see which button was pushed, it should be much easier. 我会坚持只检查看哪个按钮被按下,这应该容易得多。

Of course, it always depends on the purpose. 当然,它总是取决于目的。 However, as a rule-of-thumb, I do the natural thing: 但是,作为经验法则,我自然地做:

  • If there are form fields that would be submitted by either submit button , use Javascript and detect the submit button pressed with a handler. 如果存在任何一个提交按钮都可以提交的表单字段,请使用Javascript并检测使用处理程序按下的提交按钮。
  • In all other cases , use different forms. 所有其他情况下 ,请使用不同的形式。

I try to avoid having merged forms (the first option) as: 我尽量避免合并表单(第一个选项)为:

  1. An input name change affects both of the submit target pages, and requires changes on both of the <form action="blah"> pages. 输入名称的更改会影响两个提交目标页面,并且需要在两个<form action="blah">页面上都进行更改。
  2. Javascript is not always enabled on browsers 并非总是在浏览器上启用Javascript

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM