简体   繁体   English

一表格二提交按钮

[英]One form two submit buttons

I have two similar forms on a site that I'd like to merge into one form with two submit buttons. 我在网站上有两种相似的表单,我想通过两个提交按钮合并为一种表单。 They use most of the same data. 他们使用大多数相同的数据。

The first form uses GET to send the data to another server. 第一种形式使用GET将数据发送到另一台服务器。 The second form sends it in an email. 第二种形式通过电子邮件发送。 I'd like to strongly encourage site users to use option one before trying option two. 我强烈建议网站用户在尝试方法二之前先使用方法一。

I know how to do this with javascript, but not in a way that degrades well. 我知道如何使用javascript做到这一点,但不会以降低效果的方式实现。 Any other ways to have two submit options? 还有其他两种提交方式吗? Or other ideas for how to accomplish this? 或其他想法如何做到这一点? Thanks! 谢谢!

Snow Blind provided the good solution, but you can't determine which button was clicked. Snow Blind提供了很好的解决方案,但是您无法确定单击了哪个按钮。
Buttons must have different names, not the same. 按钮必须具有不同的名称,而不是相同的名称。

Example: 例:

<input type="submit" name="server" value="Server" />
<input type="submit" name="email" value="Email" />

<?php

if(isset($_GET['server']))
{
    // Send to another server
}
else if(isset($_GET['email']))
{
    // Send to email
}
else die("None of buttons was clicked.");

?>

Additionally, if you have a same code in both parts (server and email), you can do the following: 此外,如果两个部分(服务器和电子邮件)的代码相同,则可以执行以下操作:

if(isset($_GET['server']) || isset($_GET['email']))
{
    // Do something common to both methods
    if(isset($_GET['server'])) 
    {
        // Send to server
    }
    else 
    {
        // Send to email
    }
}




Better solution, in my opinion, is to put only 1 submit button + a dropdown menu with method to choose. 我认为,更好的解决方案是只放置1个提交按钮+一个带有选择方法的下拉菜单。

<select name="sendMethod">
    <option value="" disabled>Choose sending method...</option>
    <option value="server">Send to another server</option>
    <option value="email">Send to e-mail</option>
</select>

Like everybody said use either checkbox/radio button. 就像每个人所说的,使用复选框/单选按钮。 And set the one that use GET as the default option 并设置使用GET作为默认选项的

If you don't want to use javascript you can always get the value and of the checkbox/radio and check user choice 如果您不想使用javascript,则始终可以获取复选框/单选的值和,并检查用户选择

You can create two submit buttons and give them the same name but different value . 您可以创建两个提交按钮,并为其指定相同的name但使用不同的value

<input type="submit" name="submit" value="Server">
<input type="submit" name="submit" value="Email">

Then you can check $_GET['submit'] or $_POST['submit'] (depending on form method ) to see which submit was used. 然后,您可以检查$_GET['submit']$_POST['submit'] (取决于form method )以查看使用了哪个提交。

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

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