Monday 29 May 2017

Sumbited data from NodeJS are undefined in Mysql Database

When i submitted the form data, the data added to mysql database and values shown as Undefined.
Can anyone show me whats the problem, I'm very new to NodeJs development.

HTML doc

<html>

    <form method="post" action="/add">

    Name : <input   name="mname" type ="text" > <p>
    Age :  <input name="mage"   type ="text" > <p>
    Department : <input name="mdepartment"   type="text" > <p>
    City : <input   name="mcity" type = "text"> <p>

    <input type="submit" value ="submit">
    <input type="reset" value="reset"> 
    </form>
</center>

and My Javascript file as follows

myapp.js

 var express = require('express');
var path = require('path');
var mysql = require('mysql');
var app = express();
var bodyparser = require('body-parser');

var connection = mysql.createConnection({
    host : 'localhost',
    user : 'root',
    password : '1234'
});

connection.query('USE samplenodejs');

app.set('port',3000);


app.use(bodyparser.urlencoded({extended : false}));

app.get('/', function(req, res) {
    console.log('GET /');
    res.sendFile(__dirname + '/AddDetails.html');
});


app.post('/add',function(req,res){

connection.query("INSERT INTO test VALUES('','"+req.body.mname+"','"+req.body.mage+"','"+req.body.mcity+"','"+req.body.mdepartment+"')",function(err,res){
    if(err) throw err;
});
res.redirect('/');

});


app.listen(app.get('port'));
console.log('Express server listening on port '+app.get('port'));



via Tharaka_Ravishan

No comments:

Post a Comment