【问题标题】:Which form has been submitted from <button> element (with type as button)<button> 元素提交了哪个表单(类型为按钮)
【发布时间】:2013-04-17 18:28:57
【问题描述】:

我进行了很多搜索,但找不到/理解解决此问题的方法。

我必须在同一页面中填写表格。

<form id="inquiry_theme_form" name="inquiry_theme_form" action"#" method="post">

                                /*Some fields etc*/

<button type="button" value="Go" id="inq_btn_sumbit" name="inq_btn_sumbit">Go</button>
</form>

<form id="booking_form" name="booking_form" action"#" method="post">

                              /*Some fields etc*/

<button type="button" name="book_btn_submit" id="book_btn_submit">Book now</button>
</form>

我将按钮的类型设置为按钮,因为我想在发布之前验证某些字段。因此,如果用户填写了所有字段,我将使用 jquery 发布表单。

$("#inq_btn_sumbit").click(function() {
var the_form = $("#inquiry_theme_form");

                /* Validation */
         if(error){
                 /*Warn the user*/
        }
        else{
            the_form.submit();
        }
});

$("#book_btn_submit").click(function() {
var the_form = $("#booking_form");

                /* Validation */
         if(error){
                 /*Warn the user*/
        }
        else{
            the_form.submit();
        }
});

在我拥有表单的同一页面上,我在页面顶部有此代码。

if($_SERVER['REQUEST_METHOD'] == "POST"){
       /*I WANT HERE TO SOMEHOW CHECK WHICH FORM HAS BEEN SUBMITTED*/
}

那么我如何识别用户按下了哪个按钮,即提交了哪个表单

对不起我的英语,谢谢你的帮助!!

【问题讨论】:

  • 在每个表单中设置一个隐藏字段,并设置不同的值,并在服务器上检查它们

标签: php jquery forms button


【解决方案1】:

您可以使用&lt;input type="hidden" name="form" value"form1" /&gt; 来查看提交的内容。

【讨论】:

  • 我通常要么这样做,要么在表单的声明 action="" url 属性中使用 url 变量。像
    。然后,我将使用 $_GET['sender'] 为我的脚本提供来自 url 的变量。
猜你喜欢
  • 2013-03-12
  • 1970-01-01
  • 2019-03-21
  • 2014-02-25
  • 1970-01-01
  • 2014-07-15
  • 2010-10-07
  • 2012-03-01
  • 1970-01-01
相关资源
最近更新 更多