Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
input from html to node.js and send it to database

Hello i have problem with getting input from html to node and send it to databse. What I am getting now is input type "undefined". So if someone could help me I would really preciate it. down here is my code html and node.js

          |  |
          |  |
          |  |
        \      /
         \    /
          \  /
           \/

HTML

<form action="database" method="POST">
  <div class="form-group" id="test">
    <label for="fname">First name</label>
    <input type="text" id="fname" placeholder="First name: " name="fname">
  </div>
  <div class="form-group">
    <label for="lname">Last name</label>
    <input type="text" id="lname" placeholder="Last name: " name="lname">
  </div>
  <div class="form-group">
    <label for="state">state: </label>
    <input type="text" id="state" name="state" placeholder="sate: ">
  </div>        
 
</form>

NODE.js

var mysql = require('mysql');
var con = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "",
    database: "test"
  });

var fname = req.body.fname;
var lname = req.body.lname;
var state = req.body.state;






con.connect(function(err) {
    if (err) throw err;
    var sql = `INSERT INTO customers (c_fname, c_lname, c_state) VALUES ("${fname}", "${lname}", "${state}")`;
    con.query(sql, function (err, result) {
    if (err) throw err;
       console.log("1 record inserted, ID: " + result.insertId);
                      
    });
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It seems you are missing crucial parts. I'd recommend going through some tutorials/videos to understand how the request-response works on the web. Since you are using Node.js, this article explains how to deal with forms using express.js as the application server.

request-response sample flow chart. source: MDN

Assuming index.html and index.js (your node.js file) is in the same folder, you could do,

const express = require('express');
var bodyParser = require('body-parser')
const app = express();
  
var urlencodedParser = bodyParser.urlencoded({ extended: false })
    
app.get('/', (req, res) => {
  res.sendFile(__dirname + '/index.html');
});
    
app.post('/', urlencodedParser, (req, res) => {
    console.log('Got body:', req.body); // add logic to insert into db here.
    res.sendStatus(200);
});
    
app.listen(3000);

Also, it seems you are missing submit button in your html.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!