我在一个表单中有两个提交按钮。我如何确定哪一个击中了服务器端?


当前回答

一个更好的解决方案是使用按钮标签提交表单:

<form>
    ...
    <button type="submit" name="action" value="update">Update</button>
    <button type="submit" name="action" value="delete">Delete</button>
</form>

按钮内部的HTML(例如..>Update<..是用户所看到的;因为有HTML提供,值是不可见的用户;它只发送到服务器。这样就不会给国际化和多种显示语言带来不便(在前一种解决方案中,按钮的标签也是发送到服务器的值)。

其他回答

你可以像这样显示按钮:

<input type="submit" name="typeBtn" value="BUY">
<input type="submit" name="typeBtn" value="SELL">

然后在代码中,你可以使用:

if request.method == 'POST':
    #valUnits = request.POST.get('unitsInput','')
    #valPrice = request.POST.get('priceInput','')
    valType = request.POST.get('typeBtn','')

(valUnits和valPrice是我从表单中提取的其他一些值,用于演示)

处理多个提交按钮的最佳方法是在服务器脚本中使用切换案例

<form action="demo_form.php" method="get">

    Choose your favorite subject:

    <button name="subject" type="submit" value="html">HTML</button>
    <button name="subject" type="submit" value="css">CSS</button>
    <button name="subject" type="submit" value="javascript">JavaScript</button>
    <button name="subject" type="submit" value="jquery">jQuery</button>
</form>

服务器代码/服务器脚本-你提交表单的地方:

文件demo_form.php

<?php
    switch($_REQUEST['subject']) {

        case 'html': // Action for HTML here
                     break;

        case 'css': // Action for CSS here
                    break;

        case 'javascript': // Action for JavaScript here
                           break;

        case 'jquery': // Action for jQuery here
                       break;
    }
?>

来源:W3Schools.com

您可以在一个表单中设置多个提交按钮 例子:

 <input type="submit" name="" class="btn action_bg btn-sm loadGif" value="Add Address" title="" formaction="/addAddress"> 
 <input type="submit" name="" class="btn action_bg btn-sm loadGif" value="update Address" title="" formaction="/updateAddress"> 

简单。你可以在不同的提交按钮上点击改变表单的动作。

在文档中试试这个。准备好:

$(".acceptOffer").click(function () {
    $("form").attr("action", "/Managers/SubdomainTransactions");
});

$(".declineOffer").click(function () {
    $("form").attr("action", "/Sales/SubdomainTransactions");
});

由于您没有指定使用的服务器端脚本方法,因此我将给出一个适用于PHP的示例

<?php if(isset($_POST["loginForm"])) { print_r ($_POST); // FOR Showing POST DATA } elseif(isset($_POST["registrationForm"])) { print_r ($_POST); } elseif(isset($_POST["saveForm"])) { print_r ($_POST); } else{ } ?> <html> <head> </head> <body> <fieldset> <legend>FORM-1 with 2 buttons</legend> <form method="post" > <input type="text" name="loginname" value ="ABC" > <!--Always use type="password" for password --> <input type="text" name="loginpassword" value ="abc123" > <input type="submit" name="loginForm" value="Login"><!--SUBMIT Button 1 --> <input type="submit" name="saveForm" value="Save"> <!--SUBMIT Button 2 --> </form> </fieldset> <fieldset> <legend>FORM-2 with 1 button</legend> <form method="post" > <input type="text" name="registrationname" value ="XYZ" > <!--Always use type="password" for password --> <input type="text" name="registrationpassword" value ="xyz123" > <input type="submit" name="registrationForm" value="Register"> <!--SUBMIT Button 3 --> </form> </fieldset> </body> </html>

形式

当点击Login -> loginForm

当点击Save -> saveForm

当点击注册->注册表单