Processing multiple forms in one page using Mongodb and node.js Processing multiple forms in one page using Mongodb and node.js mongoose mongoose

Processing multiple forms in one page using Mongodb and node.js


Well, first off because you have two forms, you have to specify which form to submit on a button click.Right now any button click would submit just the first form.Give first form's submit button class registerButton and second form's submit class loginButton.I would also suggest giving classes to your forms. One will be registerForm, another is loginForm.Then change your HTML to reflect the changes

<form method="POST" action="/samples" class="registerForm"> ...<button type="submit" class="btn btn-primary registerButton">Sign in</button><form method="POST" action="/dashboard" class="loginForm">...<button type="submit" class="btn btn-primary loginButton">Log in</button>

Then change your javascript to this:

    $('button.registerButton').click( function() {        $('form.registerForm').submit();    });    $('button.loginButton').click( function() {        $('form.loginForm').submit();    });

Now you are able to submit two forms to two different routes.
One form would go to samples and the other to dashboard, so on your server (in Node) you can check which route it is.I would suggest using one of the Node frameworks to work with your application.You can try Express. It will drastically simplify your work with routes.It will allow you to write routes as easy as this:

app.post('/samples', function(req, res){    // check register form    // res.send('user' + req.body);});app.get('/dashboard', function(req, res){    // check login form    // res.send('user' + req.body);});

You can read more on Express from a suggested list here.


You can add method="POST" and formaction="/samples" (and formaction="/dashboard" respectively) to the button tag, then the button is not confused to which endpoint it sends the form.

<button method="POST" formaction="/samples" type="submit" class="btn btn-primary">Sign in</button>

You can read more about it here.


If you are working with nodejs handlebars and controller

  1. My two form signature in a html/handlebars file :

    <form action="/user/login/logData" method="POST"><form action="/user/login/regData" method="POST">
  2. In my app.js/server.js file am added a controller like bellow

    const userAuthController=require("./controllers/loginregister.controller")app.use("/user",userAuthController)
  3. My routes in controller file(./controllers/loginregister.controller.js)

    const express=require("express")const router=express.Router();router.get("/login",(req,res) => {    console.log("page displayed")    res.render("../views/users/registration");})router.post("/login/logData",(req,res) => {    console.log("login")    res.render("../views/users/registration");})router.post("/login/regData",(req,res) => {    console.log("register")    res.render("../views/users/registration");})module.exports = router;

Note: you can use code inside the specific post method to process the form data using mongoose.