Get Posted Data From Form
node.js
Solution
You have to specify a `name` on your `<input>` element like this:
<input name="txtInput" type="text" id="txtInput">
only those with `name` set get posted.
Example:
var http=require('http');
var util=require('util');
var querystring=require('querystring');
var server=http.createServer(function(req,res){
if (req.method=='GET'){
res.end('<form action="/Home/Index" method="POST" name="form1">Enter Text:<input name="txtInput" type="text" id="txtInput"/><button type="submit" id="btnPost">Post Data</button></form></body></html>');
}else{
var chunk = '';
req.on('data', function (data) {
chunk += data;
});
req.on('end', function () {
console.log(chunk + "<-Posted Data Test");
res.end(util.inspect(querystring.parse(chunk)));
});
}
}).listen(8080);
To parse the formdata just use querystring.parse(str, [sep], [eq]) (like in the example above)
Example:
querystring.parse('foo=bar&baz=qux&baz=quux&corge')
// returns
{ foo: 'bar', baz: ['qux', 'quux'], corge: '' }
Problem
Here is a html form which posts: ``` <html> <body> <h1>Home Html Page</h1> <a href="/Product/Index">Go To Product</a> <hr/> <form action="/Home/Index" method="POST" name="form1"> Enter Text: <input type="text" id="txtInput"/> <button id="btnPost">Post Data</button> </form> </body> </html> ``` Here is JavaScript code which should get posted data: ``` function Server(req, resp) { if (req.method == 'POST') { var chunk = ''; req.on('data', function (data) { chunk += data; }); req.on('end', function () { console.log(chunk + "<-Posted Data Test"); }); }' ... resp.write(...); resp.end(); } ``` Can you help me? I'm new in nodejs, so can you show me example?