user3642365
user3642365

Reputation: 579

HTML form not sending data in Express

I have the following form in HTML:

    <form method="post" id="registration-form" action="/register"> 
      <div class="form-group">
        <label for="UsernameRegistration">Username:</label>
        <input type="text" class="form-control" id="UsernameRegistration">
      </div>
      <div class="form-group">
        <label for="PasswordRegistration">Password:</label>
        <input type="password" class="form-control" id="PasswordRegistration">
      </div>
      <div class="form-group">
        <label for="ConfirmPasswordRegistration">Confirm Password:</label>
        <input type="password" class="form-control" id="ConfirmPasswordRegistration">
      </div>
      <input type="submit" class="form-control" />
    </form>

The /register endpoints looks like the following:

router.post('/register', function(req, res, next) {
  console.log(req);
});

In req.query and req.body, there is no data. What am I doing wrong?

Upvotes: 2

Views: 3693

Answers (3)

Gaurav joshi
Gaurav joshi

Reputation: 1799

I think you are missing these two things :-

1.Have you added body parser in our app ?? (Contains key-value pairs of data submitted in the request body. By default, it is undefined, and is populated when you use body-parsing middleware such as body-parser)

var app = require('express')();

var bodyParser = require('body-parser');

app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
  1. Missing name attribute in your form elements

Upvotes: 2

kailash yogeshwar
kailash yogeshwar

Reputation: 834

You haven't provided name attribute for input elements .

I you provide name attribute to element eg:

<form method="post" id="registration-form" action="/register"> 
      <div class="form-group">
        <label for="UsernameRegistration">Username:</label>
        <input name="username" type="text" class="form-control" id="UsernameRegistration">
      </div>
      <div class="form-group">
        <label for="PasswordRegistration">Password:</label>
        <input name="password" type="password" class="form-control" id="PasswordRegistration">
      </div>
      <div class="form-group">
        <label for="ConfirmPasswordRegistration">Confirm Password:</label>
        <input name="confpass" type="password" class="form-control" id="ConfirmPasswordRegistration">
      </div>
      <input type="submit" class="form-control" />
    </form>

router.post("/registration", (req, res) => {
  var username = req.params.username;
  var  pass  = req.params.password;
  var confpass = req.params.confpass;
})

You will get data in req.params object.

Upvotes: 2

Lakshmipriya Mukundan
Lakshmipriya Mukundan

Reputation: 136

<input type="password" class="form-control" id="PasswordRegistration">

Here the attribute name is not specified. It should be like

<input type="password" name="password" class="form-control" id="PasswordRegistration">

Upvotes: 7

Related Questions