submitting two forms with one button

177 Views Asked by At

I need help....

i am trying to sent multiple forms with one submit button using xmlhttlrequest. But everytime it only sends the last form.

Here is my code

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());
}

i am trying to submit forms with .submit() on the same page and get data using $_GET. But everytime it skips index 0.

1

There are 1 best solutions below

2
jithu thomas On

Try following code.

<!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>