【问题标题】:How to get value from dynamically generated <option> <select> express ejs如何从动态生成的 <option> <select> express ejs 中获取价值
【发布时间】:2020-01-21 14:05:54
【问题描述】:

首先感谢您阅读并尝试解决我的问题。

我动态呈现了我的 {dropdown},但我无法在后端获取选定的值。

在我的表单上,我也得到了照片,并且套件的名称是动态呈现的。

如果我做错了什么,下面的代码。

语言 - Javascript FW - Express Js 模板 - EJS

我有 body-parse 并设置为 true。

  • 问题是我无法使用 POST 请求(未定义或 null)将所选值发送到后端。

  • 但如果我使用 GET 作为操作和请求,我确实会得到选定的值。

再次感谢您。如果我不清楚,我会解释清楚。我是新的NODE JS AND EXPRESS。

-前端

<form  class="ui form" action="admin/uploadphoto" method="POST" enctype="multipart/form-data">      

      <h2 class=" ui dividing header" style="text-align: center;">Add Images to suites</h2>

      <div class="two fields">
        <div class="field">
          <label>Add Suites Images</label>
            <input name="suiteimage" type="file">
        </div>
        <div class="field">
          <label>Suites*</label>
          <select name="suiteselected" class="ui dropdown">
            <% suites.forEach(function(suite) { %>     
              <option name = "suiteid" value="<%= suite.suite_name %>"><%= suite.suite_name %></option>
              <% })%>

          </select>
        </div>
      </div>
      <input type="submit" class="ui button" tabindex="0" value="Save New Suite">
    </form>
  • 后端
const uuid = require("uuid/v4");
const { Pool } = require("pg");
const multer = require("multer");


// Insert photo and selected value from dropdown (options)
const PostPhoto = (req, res) => {
  var suiteimage;
  const suite_photo_id = uuid(); 
  const {suiteselected} = req.body;
  console.log( suiteselected + " --ID") //Here i am testing if selected valued is passed

// this is my multer function to config where i need to store my photo path
  upload(req, res, err => {
     suiteimage = req.file.path;
    if (err) {
      console.log(err);
    } else {
      console.log(suiteimage);
    }
  });


..... //my database query to save my post....

};

-路线

router.post ('/uploadphoto', service.PostPhoto);
``

【问题讨论】:

  • 您可以修改您的帖子以包含函数 uuid() 吗?
  • 嗨@SPlatten,感谢您的回复。我刚刚修改了它,但是它是一个已安装的 uuid();包

标签: javascript node.js express visual-studio-code ejs


【解决方案1】:

我已经找到了解决方案,应该需要 body-parse 并将其扩展为 true。

当将文本和图像作为 1 个表单发布到后端时,请确保

  • 您的图像函数(上传)在 req,res 函数中,否则值将是 undefine 或 null。

如果您需要更多解释来发表评论。

//my post for image and text function
const PostPhoto = (req, res) => {
   const suite_photo_id = uuid(); 

  upload(req, res, function(err) {
    const {suiteselected} = req.body;
    console.log(req.file);
    var imagePath = req.file.path.replace(/^public\//, '');
    console.log( suiteselected + " --ID")
   console.log(imagePath);


   pool.query(
    "INSERT INTO suite_photos (suite_photo_id,suite_photo,suite_id) VALUES($1,$2,$3)",
    [suite_photo_id, imagePath,suiteselected],
    (error, result) => {
      if (error) {
        throw error;
      } else{
        console.log(result);
        res.redirect("/admin");
      }
    }
  );
 });



};

一切都变了。

谢谢大家!!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 2016-07-26
    • 1970-01-01
    相关资源
    最近更新 更多