0

I have a local server running with Node JS. Then I have several select tags and I want after the user chooses, to save their inout on a text file? Could somebody help me out how to do this? (furthermore, when I press submit I get an undefined on the terminal)

The node js code:

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
var port = process.env.PORT || 8000;

app.use(express.static(__dirname + '/server'));
app.use(express.static(__dirname + '/public'));

app.use('/images', express.static(__dirname +'/images'));

app.get('/', function(req, res){
    res.sendfile('main.html');
});

app.listen(port, function(){
    console.log('server is running on ' + port);
});

app.post('/submit', function(req, res){
  console.log(req.body.rank);
});

The HTML:

<form method="POST" action="/submit"  > <!--action-page.php-->

        <select name="option" size="1" style="width:80px;" required id="rankx">
          <option value="" selected disabled hidden>Option</option>
          <option value="1">1</option>
          <option value="2">2</option>
        </select>

      <br />
        <select name="option" size="1" style="width:80px;" required>
          <option value="" selected disabled hidden>Rank</option>
          <option value="1">1</option>
          <option value="2">2</option>
        </select>

  <input type="submit" value="Submit" name="submit" id="submit"/>

2 Answers 2

1

Problem 1

app.use(bodyParser.json());

Your form is submitting data in application/x-www-form-urlencoded format, not JSON.

You need a body parser that can handle application/x-www-form-urlencoded.

See bodyParser.urlencoded([options])

app.use(bodyParser.urlencoded());

Problem 2

You have inputs named option and submit but you are looking for one named rank.

You have to match your input names to what you are looking for.

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

Comments

0

Use this code inside the node js file

const bodyParser = require('body-parser');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded\

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.