MSH MSH - 1 year ago 48
Javascript Question

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

I am having my sign in and registration form both in the same page. I have specified two different routes for posting the data but it saves sign-in form inputs (which are none) when I hit the register button at the end of the registration form? I looked at this question(Multiple forms and one processing page). How would it work with node, would that means I have to create a module for different forms?

Here is my code. I have a small script at the end also because the slider function of my bootstrap template was clashing with the submit function.

<form method="POST" action="/samples">
<input type="text" name="fullName" id="fullName" class="input-lg " placeholder="your full name" data-required="true">
<input type="text" name="age" id="age" class="input-lg" placeholder="age">
<input type="text" name="city" id="city" class="input-lg" placeholder="Your City">
<input type="text" name="job" class="input-lg" placeholder="Your Job">
<input type="text" name="username" class="input-lg " placeholder="prefered username" data-required="true">
<input type="password" name="password" class="input-lg" placeholder="Password">
<input type="email" name="email" class="input-lg " placeholder="your email" data-required="true">
<button type="submit" class="btn btn-primary">Sign in</button>

<form method="POST" action="/dashboard">
<hr class="colorgraph">
<input type="email" name="emaillog" class="input-lg " placeholder="your email" data-required="true">
<input type="password" name="passwordlog" class="input-lg" placeholder="Password">
<button type="submit" class="btn btn-primary">Sign in</button>

$('button').click( function() {

Thanks for your help

Answer Source

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() {
    $('button.loginButton').click( function() {

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:'/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.