【问题标题】:submitting two forms with one button一键提交两份表格
【发布时间】:2020-08-24 21:17:34
【问题描述】:

我需要帮助....

我正在尝试使用 xmlhttlrequest 通过一个提交按钮发送多个表单。但每次它只发送最后一个表单。

这是我的代码

for(j=0;j<collection;j++)
{   

xhr.open("POST", "showQuestionnaire.php",true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(document.getElementById("submitAnswers"+j).submit());
}

我正在尝试在同一页面上使用 .submit() 提交表单并使用 $_GET 获取数据。但每次它都会跳过索引 0。

【问题讨论】:

  • 我们还需要查看您的 html 部分

标签: javascript php forms xmlhttprequest multiple-forms


【解决方案1】:

试试下面的代码。

<!DOCTYPE html>
<html>
<head>
    <title>Multiform - JAVASCRIPT</title>
</head>
<body>

<fieldset>
    <legend>Form 1</legend>
    <form name="f1" id="f1" onsubmit="return validate(this)">
        <input type="text" name="username" placeholder="Username" />
    </form>
</fieldset>

<fieldset>
    <legend>Form 2</legend>
    <form name="f2" id="f2" onsubmit="return validate(this)">
        <input type="text" name="email" placeholder="Email" />
    </form>
</fieldset>

<fieldset>
    <legend>Form 3</legend>
    <form name="f3" id="f3" onsubmit="return validate(this)">
        <input type="text" name="password" placeholder="Password" />
    </form>
</fieldset>

<button onclick="submitAll();">SUBMIT ALL</button>

<script>
'use strict';

function validate(form){
    //forms processsing goes here...
    console.log(form, form.name)
    return false;
}

function submitAll(){
    for(var i=0, n=document.forms.length; i<n; i++){
        alert(i);
        document.forms[i].onsubmit();
    }

}

</script>

</body>
</html>

【讨论】:

  • 什么都没有发生.....它甚至没有提交表单......
  • 如果需要,您必须设置表单操作并设置参数,然后只有您的端点会受到打击。
猜你喜欢
  • 2021-09-30
  • 2018-09-12
  • 2019-10-23
  • 1970-01-01
相关资源
最近更新 更多