Problem responding to html select option(value)

I cant seems to respond to select option from html form. the page is always blank any time i post.
Does it mean that the method is different from other form like email, password etc.

HTML/PUG
DOCTYPE
html(lang="en")
  head
    title profile page
    meta(name='description', content='Preparing Personal Portfolio')
    meta(charset='utf-8')
    meta(http-equiv='X-UA-Compatible', content='IE=edge')
    meta(name='viewport', content='width=device-width, initial-scale=1')
  body
    div.profile-page
      h1.center#Welcome welcome, #{username}!
      p This is your profile page where your personal data, and transaction can be made
    div.file-form
      form(action="/file" method="post")
        div.label
          label choose file
          input(type="file", name="file")
          br
          input(type="submit", value="submit")
    hr      
    div.message-form
      form(action="/messages" method="post")
        div.dropdown-button
          label(for="manager") Choose Pass-Request Manager   
          br
          select(name="manager") 
            option(disabled, selected) Choose Manager
            option(value="firstManager") firstManager
            option(value="secondManager") secondManager
          br
        div.text-area
          label(for="message") Compose Text in the Text-Area 
          br
          textarea(type="text" name="message" id="message" placeholder="Text here")
          br
          input(type="submit", value="submit")
              
           
              
              
      a(href="/logout") logout
    

Server.js

const express = require("express");
const path = require("path");
const {handleReg,  handleLogin, handleMessage, handleLogout} = require("../controller/auth");
const {loginEng} = require("../passport/index");
const passport = require("passport");
//const local = require("../passport/local");
const {registerValidator,
       loginValidator,
       validationErrorsHandler} = require("../middleware/validator");

module.exports = function(app){
  
  app.route("/").get(function(req, res){
    res.render(__dirname + "/../views/index.pug");
  });
  
  app.route("/register").get( function(req, res){
    res.render(__dirname + "/../views/regForm.pug");
  });
  
  //ensure login helper function
  function ensureAuthentication(req, res){
    let isAuth = req.isAuthenticated();
    if(!isAuth) throw " Error authenticating login";
  };
  
  app.route("/login").get( function(req, res){
    res.render(__dirname + "/../views/login.pug");
  });
  
  app.route("/profile").get(ensureAuthentication, function(req, res){
    res.render(__dirname + "/../views/profile.pug");
  })
  
  app.route("/register").post([...registerValidator,  validationErrorsHandler], handleReg);
   
  app.route("/login").post( passport.authenticate('local', { session: false }),  handleLogin);
  
  app.route("/messages").post(function(req, res){
 
  console.log(req.body.firstManager);
  res.send(req.body.firstManager); 
});
  
  app.route('/logout').get(handleLogout);
  
  
}    

project link: Glitch

Hi, I think you need to tell your app to use the urlencoded middleware, to decode your form, otherwise the values are not pulled through into request.body.

Here is an article explaining it: Handling forms in Express

And here is the relevant code segment:

app.use(express.urlencoded({
  extended: true
}))

I hope it helps!

1 Like

when the user selects an option such as firstManager, it adds this key-value pair to the form:

manager=firstManager

youā€™d need something like req.body.manager === 'firstManager' rather than req.body.firstManager

2 Likes

Hi SteGriff, thanks it really help. please can you explain something or rather send some Doc for mean am really stock here.
i want a user to send message to the manager and received message from the manager, also the manager should have access to all the users, basically i was trying to use the fact that in the model i reference Manager:{
type:Schema.Types.ObjectId
ref: user
}
base on the document i saw but i really dont understand how that works completely can you help?