Reputation: 15303
I am a beginner in node.js. I am trying to add a 'name' and 'department' using a form. once i click submit, i am consoling both 'name' and 'department' but I am getting 'both' are 'undefined'.
I don't know where is the worng I did. any one figure out and help me to console the name and department?
here is my form:
<form method="post" role="form" class="form-horizontal" enctype='multipart/form-data'>
<div class="form-group">
<label for="name"><input id='name' name='name' placeholder='Enter your name' type="text" ></label>
<label for="dept"><input id='dept' name='dept' placeholder='Enter your department here' type="text"></label>
<button type='submit' class="btn btn-default" id='submit'>Submit your Details</button>
<div>
</form>
Here is my app.js
var
http = require('http'),
express = require('express'),
routes = require('./routes'),
userList = require('./routes/list'),
path = require('path'),
app = express();
app.set('port', process.env.PROT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(app.router);
app.use(express.static(path.join(__dirname, 'static')));
app.get('/', routes.list);
app.get('/addList', userList.addList);
app.post('/addList', function(req, res){
var name = req.params.name;
var dept = req.params.dept;
console.log(name, dept);//getting undefined why?
});
http.createServer(app).listen(app.get('port'), function(){
console.log('successfuly initiated!');
});
updated form and app.js
Form:
<form method="post" role="form" class="form-horizontal" action="/addList">
<div class="form-group">
<label for="name"><input id='name' name='name' placeholder='Enter your name' type="text" ></label>
<label for="dept"><input id='dept' name='dept' placeholder='Enter your department here' type="text"></label>
<p><input type='submit' class="btn btn-default" id='submit' value='Submit your Details'/></p>
<div>
app.js
var
http = require('http'),
express = require('express'),
routes = require('./routes'),
userList = require('./routes/list'),
path = require('path'),
app = express();
app.set('port', process.env.PROT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(app.router);
app.use(express.static(path.join(__dirname, 'static')));
app.use(express.bodyParser()); //newly added
app.use(express.urlencoded()); //newly added
app.get('/', routes.list);
app.get('/addList', userList.addList);
app.post('/addList', function(req, res){
var name = req.body.name;
var dept = req.body.dept;
console.log(name, dept);
});
http.createServer(app).listen(app.get('port'), function(){
console.log('successfuly initiated!');
});
Upvotes: 1
Views: 1304
Reputation: 7866
Your form is missing action="/addList"
attribute
also since you are posting with method POST
your values will be on the body
var name = req.body.name;
var dept = req.body.dept;
for the values to appear in body add this middleware
app.use(express.bodyParser());
app.use(express.urlencoded());
app.use(app.router)
should be the last thing you call.
Upvotes: 4