I tried to answer my question here: Submit two forms with one button (but couldn't get it solved)
I have a HTML page displaying a form. I can click an "add more" button and append another form to the page. I want to be able to submit all those forms with a single button.
The initial form has this header:
<form class = "aform" method = 'POST' action="http://localhost:8080" >
I use this button to submit all the forms:
<input type = "button" value = "Submit" onclick="submitForms()"/>
This is the submitForms() function:
function submitForms() {
var allforms = document.getElementsByClassName('aform');
for(var i = 0; i < allforms.length; i++)
allforms[i].submit();
}
The problem is: only the last form is sent to the json file. If there's only one form, it works fine; if there are two, only the second is sent.
The code is huge, so I'm not sure if I've posted everything relevantt.
EDIT: This is the node.js request
else if (req.method === 'POST' && req.url === '/'){
var body ='';
req.on('data', function(data){
body += data.toString();
});
req.on('end', function(){
var postObj = qs.parse(body);
console.log(postObj);
fs.appendFileSync('./data/users.json', JSON.stringify(postObj) + "\n ", function(err) {
if (err) throw err;
console.log('Saved!');
res.end();
});