【问题标题】:Multiple form submits from one button? [duplicate]从一个按钮提交多个表单? [复制]
【发布时间】:2014-07-10 14:30:34
【问题描述】:

如何通过一个提交按钮将多个表单SUBMITTED/POSTED 发送到 PHP?有没有一种简单的html方法来实现这一点?我需要在 PHP 端使用传统的 $_POST 变量,这就是原因。

例子

<form class="form-horizontal" method="post">

    // SOme fields

</form>

<form class="form-horizontal" method="post">

    // SOme more fields

</form>


<form class="form-horizontal" method="post">

    // some fields here

    <button type="submit" class="btn btn-success">Stuff</button> // This button posts all three forms!
</form>

【问题讨论】:

  • 不到 10 秒的投票?这怎么可能?
  • 不,提交只适用于它嵌套的表单。你将需要ajax。 stackoverflow.com/questions/8563299/…
  • 为什么一个页面需要三个表单?这似乎是个坏主意。为什么不能在一个表单中发布所有字段?
  • 问题是我的其他字段处于模态。模态框需要保持在页面顶部,所以我不能只将它们添加到表单中。
  • 为什么不呢?将它们分组到一个字段集或一个 div 中,并将其用于您的模态......

标签: php html forms


【解决方案1】:

你可以这样做:

<form class = "form-horizontal" method = "post>
    <select id = "select" name = "selection">
        <option name = "option_one" value = "html">html</option>
        …
    </select>

    <input type = "checkbox" name = "selected[]" value = "first_value">first</input>
    <input type = "checkbox" name = "selected[]" value = "second_value">second</input>

    <input type = "checkbox" name = "another_selected[]" value = "new_first">another first</input>
    <input type = "checkbox" name = "another_selected[]" value = "new_second">another second</input>
    <input id = "submit" name = "submit" type = "submit"></input>
</form>

然后在 PHP 中:

$selected = _POST["select"];
$first_checkbox_group = _POST["selected"];
$second_checkbox_group = _POST["another_selected"];

只要确保你给你不同的部分不同的名字。

【讨论】:

  • 我想我明确说过我想要三个表格...
  • 我会为这项努力投票。