отправка двух форм с помощью одной кнопки - PullRequest
0 голосов
/ 09 мая 2020

Мне нужна помощь ....

Я пытаюсь отправить несколько форм с помощью одной кнопки отправки, используя 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.

1 Ответ

0 голосов
/ 09 мая 2020

Попробуйте следующий код.

<!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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...