简体   繁体   English

如何访问 html 表单中的输入表单结果?

[英]How can I access the input form results in an html form?

I am trying to import my project into a nodejs app where I will be able to run the website on a localhost.我正在尝试将我的项目导入到 nodejs 应用程序中,我将能够在本地主机上运行该网站。 This works, because when I run index.js and enter the url 'http://localhost:8080/', it redirects me to the homepage of my website.这是可行的,因为当我运行 index.js 并输入 url 'http://localhost:8080/' 时,它会将我重定向到我网站的主页。

The problem is, I have a form on my website, and I am trying to access the Feedback.html page where the form resides.问题是,我的网站上有一个表单,我正在尝试访问该表单所在的 Feedback.html 页面。 What I am trying to do is upon submission, the form data is returned, and the data prints to the terminal (console.log()).我要做的是在提交时返回表单数据,并将数据打印到终端(console.log())。 If you look at my code, I believe that it is right.如果您查看我的代码,我相信它是正确的。 However, I am not sure where I need to place my Project4 directory.但是,我不确定我需要将我的 Project4 目录放在哪里。 Should I place it in my views folder?我应该把它放在我的视图文件夹中吗?

I am confused on why I need a views folder.我对为什么需要视图文件夹感到困惑。 Also, my form submission code is unresponsive.另外,我的表单提交代码没有响应。

const path = require('path');
const fs = require('fs');
const bodyParser = require('body-parser');
const { render } = require('pug');

const app = express();

//middleware and routing
app.set('view engine', 'pug');
app.use(bodyParser.urlencoded({ extended: true }));

//Viewing website
app.use('/Project4', express.static('Project4'));
app.get('/', function(req, res){
    res.redirect('/Project4/index.htm');
});
//------------------------------

//***FORM SUBMISSION PART***
app.get('/Project4/Feedback.html', function(req, res){
    res.render('Project4/Feedback.html');
});
app.post('/submit-form', function(req, res){
    console.log(req.body);
    res.end();
});
//------------------------------

const PORT = process.env.PORT || 8080;

app.listen(PORT, function(error){
    if(error){
        console.log('Issue with server on port ' + PORT);
    }
    else{
        console.log('Server running on port ' + PORT);
    }
}); ```

[![This is what my app folder looks like. Where do I place the Project4 folder so that I can access its form via post method?][1]][1]


  [1]: https://i.stack.imgur.com/CzC8p.png

In your form (please include the form as well), the name of the thing you want to access is very important.在您的表单中(也请包括表单),您要访问的事物的名称非常重要。 Using a npm package called body-parser (do npm i body-parser then const bodyParser = require("body-parser") . This basically just extracts the entire body portion of an incoming request stream and exposes it on req.body. Now you have body parser setup, you just need the name of the input (eg feedback) and do this Using a npm package called body-parser (do npm i body-parser then const bodyParser = require("body-parser") . This basically just extracts the entire body portion of an incoming request stream and exposes it on req.body. Now您已经设置了正文解析器,您只需要输入的名称(例如反馈)并执行此操作

console.log(req.body.feedback)

in the app.post route.在 app.post 路线中。 And you should be set, The thing i should mention though is that the form should have the method of POST.你应该设置好,我应该提到的是表单应该有POST的方法。 the route is correct and the button is a SUBMIT button.路线正确,按钮是提交按钮。 Here is what I would have done.这是我会做的。

The form (HTML)表单 (HTML)

<form  action="/" method="post">
  <input type="text" name="feedback" placeholder="Your Feedback">
  <button type="submit">Submit feedback</button>
</form>

App.js应用程序.js

const express = require('express');
const bodyParser = require("body-parser");
const app = express();
//Those were the modules
app.use(bodyParser.urlencoded({ extended: true }));
//use body parser with the urlencoded extended
app.get("/", function(req, res){
  res.sendFile(__dirname + "/index.html");
});
//sends the index.html when going to home route (/)
app.post("/", function(req, res){
  var feedback = req.body.feedback;
  console.log(feedback);
  res.redirect("/")
});
// when posted, we make a variable requesting the body, and then the name of the input, which is the name part of the input.
app.listen(3000, function(req, res){
  console.log("Server started on port 3000");
}); //self-explanatory.

One last thing.最后一件事。 Your views folder is needed because you are using a view engine.需要您的 views 文件夹,因为您使用的是视图引擎。 Take EJS;以EJS; if I used res.render(index, {feedbackstuff: feedback}) , then the index file would need to be a.ejs file and in the views folder.如果我使用res.render(index, {feedbackstuff: feedback}) ,那么索引文件将需要是 a.ejs 文件并位于 views 文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM