2

I am new to node js and i am stuck to send the selected drop down element (like java,angular see below ex ) from client to server using node js - express. When i try to print it in console. system displays an error message "undefined " for drop down elements. Please help me on this. Thanks in advance.

EJS code

<form method="POST" action="/viewreg.html">
<select name="Courses" form="Courses">
<option value="Java">Java</option>
<option value="Angular">Angular</option>
<option value="Automation">Automation</option>
</select>
<input type='submit'name ='register' onclick="form.action='viewreg.html'"
</form>`

Server side -

const http = require('http');
const express = require('express');
const bodyparser=require('body-parser');
const path=require('path');
const hostname = 'x.x.x.x'
const port = 80;
const app = express();
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({extended:true}));
app.use(express.static(path.join(__dirname,'public')));
app.set('view engine','ejs');
app.set('views',path.join(__dirname,'views'));
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({extended:true}));
const dir=path.join(__dirname,'public');
app.set('view engine','ejs');
app.set('views',path.join(__dirname,'views'));

app.post('/viewreg.html',function(req,res) {
const newuser= {
        emp_name:req.body.emp_name, 
        emp_id:req.body.emp_id, 
        phone_no:req.body.phone_no, 
        Team:req.body.selectpicker,
        courses:req.body.Courses,
    }
console.log(newuser);
});
2
  • Why you define onclick="form.action='viewreg.html'" for the submit button? It does not make any sense. The action URL is already defined in the form Commented Sep 4, 2017 at 6:33
  • Also, when POST /viewreg.html request is sent, have you checked the DevTool of the browser? Is the POST request sent? Is the selected element value sent through the HTTP request? Node.js is the technology to receive the request. For "how to send...", you need to check browser side code. Commented Sep 4, 2017 at 6:36

1 Answer 1

3

You need to revisit the below issues which exist in your code.

  • Change your action url from action="/viewreg.html"> to action="/viewreg">
  • There is no need to have onclick="form.action='viewreg.html'" in your submit button.

  • You don't have an ID attribute attached to your <select> tag
    where as you have a form attribute incorrectly over there.

You need to re-write your ejs tempalte as,

<form method="POST" action="/viewreg">
    <select name="Courses" id="Courses">
        <option value="Java">Java</option>
        <option value="Angular">Angular</option>
        <option value="Automation">Automation</option>
    </select>
    <input type='submit'name ='register'>
</form>

Also try changing your console.log(newuser); as,

console.log(req.body.Courses);

which will resolve your issue.

Hope this helps!

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

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.