简体   繁体   English

使用 php 中的一个按钮将多个 forms 提交到同一操作页面

[英]Submitting multiple forms to same action page using one button in php

So I am trying to send data from multiple forms on the same page to the same action page using only one submit button.因此,我尝试仅使用一个提交按钮将来自同一页面上的多个 forms 的数据发送到同一操作页面。

So far I have tried using the form ID for all my forms and it is still unsuccessful.到目前为止,我已经尝试为我的所有 forms 使用表单 ID,但仍然不成功。 The first form is the only one that goes thru and I think that is because the action on its FORM tag, is executed before the rest of the forms can also be executed.第一种形式是唯一通过的形式,我认为这是因为其 FORM 标签上的操作是在 forms 的 rest 也可以执行之前执行的。 Below I have put some sample code illustrating what I am try to do:下面我放了一些示例代码来说明我正在尝试做的事情:

<form method="post" action="example.php" id="formID">
//Some code here
</form>

<form method="post" action="example.php" id="formID">
//Some code here
</form>

<form method="post" action="example.php" id="formID">
//Some code here
</form>

<button type="submit" name="sub1" form="formID">Submit</button>

So how can I submit all three forms to the same page, using PHP?那么如何使用 PHP 将所有三个 forms 提交到同一页面?

Thank you for you help.谢谢你的帮助。

Technically you are submitting all the forms, but they all have the same action and ID.从技术上讲,您提交的是所有 forms,但它们都具有相同的操作和 ID。 Check out this question: multiple-forms-and-one-processing-page看看这个问题: multiple-forms-and-one-processing-page

Firstly the HTML ID attribute must be unique.首先,HTML ID 属性必须是唯一的。 Once each form has it's own ID you can then properly parse the data in the $_GET variable from within your example.php file's action.一旦每个表单都有自己的 ID,您就可以从 example.php 文件的操作中正确解析 $_GET 变量中的数据。

You can do this only using javascript and send the 3 request in the background.您只能使用 javascript 执行此操作并在后台发送 3 请求。

For example.例如。 Mark your forms with specified class, for example multiple_form .用指定的 class 标记您的 forms,例如multiple_form

Then change your code to something like:然后将您的代码更改为:

<form method="post" class="multiple_form" action="example.php" id="formID">
//Some code here
</form>

<form method="post" class="multiple_form" action="example.php" id="formID">
//Some code here
</form>

<form method="post" class="multiple_form" action="example.php" id="formID">
//Some code here
</form>

<button class="multiple_form_sender" name="sub1">Submit</button>

Then add javascript to your code然后将 javascript 添加到您的代码中

var forms = document.getElementsByClassName("multiple_form");

var sender = function(e) {
  const form = e.target;

  // Post data using Fetch
  fetch(form.action, {
    method: form.method,
    body: new FormData(form)
  })

  // Prevent the default form submit
  e.preventDefault();
};

for (var i = 0; i < forms.length; i++) {
    forms[i].addEventListener('submit', sender);
}

document.getElementById("multiple_form_sender").addEventListener('click', function(){
    for (var i = 0; i < forms.length; i++) {
        forms[i].submit();
    }
});

But if you don't want to send forms using javascript.但是,如果您不想使用 javascript 发送 forms。 You should consider one form with array parameters.您应该考虑一种带有数组参数的形式。

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

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