当前位置:优学网  >  在线题库

从视图中检索数据。带有NODEJS POST请求的EJS表单

发表时间:2022-07-04 01:39:29 阅读:51

Here is the code for my simple form

<form action="/form" id="myForm" method="POST">
      <input
        type="text"
        name=""
        id="searchBox"
        placeholder="Enter Search Term"
        required
      />
      <label for="pricingOptions">Choose Pricing</label>
      <select id="pricingOptions">
        <option value="--">None</option>
        <option value="Free">price-paid</option>
        <option value="Paid">price-free</option>
      </select>
      <select id="durationOptions">
        <option value="--">None</option>
        <option value="Free">short</option>
        <option value="Paid">long</option>
        <option value="Paid">medium</option>
        <option value="Paid">extraLong</option>
      </select>
      <label for="SearchOrder">Order by :</label>
      <select id="searchOrder">
        <option value="--">Default</option>
        <option value="relevance">Relevance</option>
        <option value="most-reviewed">most-reviewed</option>
        <option value="highest-rated">highest-rated</option>
        <option value="newest">newest</option>
        <option value="price-low-to-high">price-low-to-High</option>
        <option value="price-high-to-low">price-high-to-low</option>
      </select>
      <input type="submit" id="submitButton" value="submit" />
    </form>

I am trying to get the user's input and console.log it. My end goal is to store them in variables and use them to call my API but the post request is returning an empty object.

const express = require("express");
const bodyParser = require("body-parser");
const courseRouter = express.Router();

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

courseRouter.get("/form", (req, res) => {
    res.render("form");
  });

courseRouter.post("/form", (req, res) => {
  console.log(req.body);
});
module.exports = courseRouter;
🎖️ 优质答案
相关问题