[英]Submit button for second form submits first form on page
I'm trying to include two forms on the same page. 我正在尝试在同一页面上包含两种形式。 Below is much simpler than what I want to do, but it has the same error.
下面比我想做的要简单得多,但是有相同的错误。
<div class="content-form"> <form id="b_form" name="form_b" class="form" method="post"> </form> <form id="a_form" name="form_a" class="form" method="post"> <input type="number" id="field_quantity" name="quantity" class="form-control" /> <button type="submit" form="a_form" name="__action" value="new_shipment" class="btn btn-submit btn-success">Submit</button> </form> </div>
The code snippet is actually a smarty template that gets included in a larger page. 该代码段实际上是一个聪明的模板,包含在较大的页面中。 I think the issue is contained within this code though because the rest of the page has div tags, so it's not like I have a nested form.
我认为该问题包含在此代码中,尽管因为页面的其余部分具有div标签,所以这与我具有嵌套表单并不相同。
The issue: When I click the submit button, I echo the $_POST object on the php page that handles the request. 问题:单击提交按钮时,我在处理请求的php页面上回显$ _POST对象。 Even when the quantity field is filled in, I still get
{"__action":"new_shipment"}
as the contents of the $_POST. 即使填写了数量字段,我仍然会得到
{"__action":"new_shipment"}
作为$ _POST的内容。
If I swap the order of the two forms, then the $_POST does indeed contain the quantity field. 如果我交换两种形式的顺序,则$ _POST确实包含数量字段。 Why does the submit button in the second form not submit the fields in the second?
为什么第二个表单中的“提交”按钮不提交第二个字段中的字段? How do I fix it?
我如何解决它?
So, there are different ways to handle this. 因此,有不同的方法来处理此问题。 I went here http://phpfiddle.org , clicked on the Window tab, choose HTML Form and pasted your html and added some input on the first form:
我去了http://phpfiddle.org ,单击“窗口”选项卡,选择“ HTML表单”并粘贴了html并在第一个表单上添加了一些输入:
<form id="b_form" name="form_b" class="form" method="post">
<input type="number" id="field_quantity_b" name="quantity" class="form-control" />
<button type="submit" form="b_form" name="form_b_btn" value="new_shipment" class="btn btn-submit btn-success">Submit</button>
</form>
<form id="a_form" name="form_a" class="form" method="post">
<input type="number" id="field_quantity_a" name="quantity" class="form-control" />
<button type="submit" form="a_form" name="form_a_btn" value="new_shipment" class="btn btn-submit btn-success">Submit</button>
</form>
Then I added this 然后我添加了这个
<?php
if($_POST && isset($_POST['form_b_btn'])) {
echo "FORM B ";
print_r($_POST);
}
if($_POST && isset($_POST['form_a_btn'])) {
echo "FORM A ";
print_r($_POST);
}
?>
You need different values for the name of your submit buttons. 您需要使用不同的值作为提交按钮的名称。 Then when you check the $_POST, you verify which form was submit.
然后,当您检查$ _POST时,请确认提交了哪个表单。 There are other methods, like specifying different actions and then having different files to handle them.
还有其他方法,例如指定不同的操作,然后使用不同的文件来处理它们。 This may help you: How to place two forms on the same page?
这可能对您有帮助: 如何在同一页面上放置两个表单?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.