NodeJs Html Form Save with Mysql

admin Avatar
  • Nodesave.js
  • Nodesave.html

Nodesave.Js

var express = require(“express”);

var app = express();

var path = require(“path”);

var mysql = require(‘mysql’);

var bodyParser = require(‘body-parser’);

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

app.use(bodyParser.json());

var con = mysql.createConnection({

  host: “localhost”,

  user: “root”,

  password: “”,

  database: “test”

});

app.get(‘/’,function(req,res){

  res.sendFile(path.join(__dirname+’/nodesave.html’));

});

app.post(‘/submit’,function(req,res){

  var name=req.body.name;

  var email=req.body.email;

  var description=req.body.description;

  con.connect(function(err)

   {

    var sql = “INSERT INTO form (name, email,description) VALUES (‘” + name + “‘, ‘” + email + “‘,’” +description+”‘)”;

    con.query(sql, function (err, result)

    {

    if (err) throw err;

    console.log(“1 record inserted”);

    res.end();

    });

  });

})

app.listen(3000);

console.log(“Running at Port 3000”);

Nodesave.html

<html>

<head>

    <title> test </title>

    <link rel=”stylesheet” href=”https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css”>

</head>

    <body>

        <form action=”/submit” method=”POST”>

            <fieldset>

                <label for=”name”>Name: </label>

                <input type=”text” id=”name” name=”name” autofocus />

                <br/>

                <label for=”email”>Email: </label>

                <input type=”text” id=”email” name=”email”  />

                <br/>

                <label for=”username”>User name: </label>

                <input type=”textbox” id=”description” name=”description” />

                <br/>

                <input type=”submit” value=”create profile” />

            </fieldset>

        </form>

    </body>

</html>

Tagged in :

admin Avatar

More Articles & Posts