0

I am new to nodejs.please help. Here is my html and node.js code. I want to send email and password from login.html(client) to login.js(server) and validate from table storing email and password.

login.html

<!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="/loginstyle.css" /> </head> <body> <form class="css" method="post" action="http://localhost:9383/valid" > <fieldset > <input type="text" name="email" class="inputemail" placeholder="Email" /> </fieldset> <fieldset > <input type="password" name="pass" class="inputpassword" placeholder="Password" /> </fieldset > <fieldset > <button type="submit" class="submit">Submit</button> </fieldset> </form> </body> </html> 

login.js

var express = require('express') , app = express() , mysql = require("mysql"); app.get('/loginme', function (req, res) { res.sendfile(__dirname + '/loginmysql.html'); }); app.post('/valid', function (req, res) { console.log("hello"); var username=req.body.email; var password=req.body.pass; var connection = mysql.createConnection({ "hostname": "localhost", "user": "root", "password": "123", "database": "login" }); connection.connect(); connection.query('SELECT * FROM id WHERE email=? AND password=?', [username,password], function(err, rows){ if (err){ throw err; }else{ for (var i in rows) { console.log('name: ', rows[i].name); name=rows[i].name; res.redirect('/option'); } } }); connection.end(); }); 
5
  • Eyeballing that, it looks like it should work. What is the problem you are having? What have you done to debug it or narrow down where it is failing? Commented Aug 7, 2014 at 13:01
  • The HTML5 placeholder attribute is not a substitute for the label element Commented Aug 7, 2014 at 13:02
  • on clicking submit button it is redirected to localhost:9383/valid Commented Aug 7, 2014 at 13:04
  • That's what supposed to happen. Then it should either throw an error or redirect to /option. Presumably it does the former. What error does it throw? (Make sure you look at the node.js console). Commented Aug 7, 2014 at 13:06
  • node.js console TypeError: Cannot read property 'email' of undefined Commented Aug 7, 2014 at 13:09

1 Answer 1

4

You're missing a form parsing middleware. Try adding the body-parser middleware somewhere before your POST route.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.