0

I want to get value using post method in node.js using body-parser. But I always get undefined.

Below is my node.js code.

var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false });

app.get('/form', function (req, res) {
  html += "<form action='/thank'  method='post' name='form1'>";
  html += "<div id='divParent'>";
  html += "<div id='div1' name='formset' class='formset'>";
  html += "<p>Name: <input type= 'text' name='name'></p>";
  html += "<p>Sex: Male <input type='radio' class='gender' name='gender' value='male'>"
  html += " Female <input type='radio' class='gender' name='gender' value='female'></p>"
  html += "<p>Email: <input type='text' name='email'></p>";
  html += "<p>Address:<input type='text' name='address'></p>";
  html += "<p>Mobile number:<input type='text' name='mobilno'></p>";
  html += "<p>Note:</p>"
  html += '<textarea rows="4" cols="50" name="note" id="note" form="form1"></textarea>';
  html += "</div>";
  html += "</div>";
  html += "<input id='input1' type='submit' value='submit'>";
  html += "<INPUT type='reset'  value='reset'>";
  html += "</form>";

  ...
}

app.post('/thank', urlencodedParser, function (req, res){
  var reply='';
  reply += "<br>Array length is : " + req.body.name.length;
  reply += "<br>Your name is : " + req.body.name;
  reply += "<br>Sex is : " + req.body.gender;
  reply += "<br>Your E-mail id is : " + req.body.email; 
  reply += "<br>Your address is : " + req.body.address;
  reply += "<br>Your mobile number is : " + req.body.mobilno;
  reply += "<br>Your note is : "+ req.body.note;
  console.log(req.body)
  res.send(reply);
});

I can get the input value, but not textarea value. Here's what I get when I console.log(req.body)

{ name: 'Roy', gender: 'male', email: 'roy@topscore.com', address: 'Bali', mobilno: '0821' }

Why body-parser can't get req.body.note? What's the problem?

roscoe_x
  • 609
  • 1
  • 9
  • 16

1 Answers1

1

I think the textarea is not really part of the form since you do not have a form with id "form1". Either remove the form attribute from the textarea or add the attribute id to the form.

Mihai
  • 9,526
  • 2
  • 18
  • 40