javascript - Node.js - submitting form not console the form values -
i beginner in node.js. trying add 'name' , 'department' using form. once click submit, consoling both 'name' , 'department' getting 'both' 'undefined'.
i don't know worng did. 1 figure out , me console name , department?
here 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 name' type="text" ></label> <label for="dept"><input id='dept' name='dept' placeholder='enter department here' type="text"></label> <button type='submit' class="btn btn-default" id='submit'>submit details</button> <div> </form>
here 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 , 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 name' type="text" ></label> <label for="dept"><input id='dept' name='dept' placeholder='enter department here' type="text"></label> <p><input type='submit' class="btn btn-default" id='submit' value='submit 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!'); });
your form missing action="/addlist"
attribute
also since posting method post
values on body
var name = req.body.name; var dept = req.body.dept;
for values appear in body add middleware
app.use(express.bodyparser()); app.use(express.urlencoded());
app.use(app.router)
should last thing call.
Comments
Post a Comment