Javascript 使用express时如何访问表单中的ejs数据
在下面的代码中,我根据用户请求显示以下HTML文件。我还想访问clubname并在我的app.js文件中的app.post()函数中键入datavaluesJavascript 使用express时如何访问表单中的ejs数据,javascript,html,node.js,express,ejs,Javascript,Html,Node.js,Express,Ejs,在下面的代码中,我根据用户请求显示以下HTML文件。我还想访问clubname并在我的app.js文件中的app.post()函数中键入datavalues <!DOCTYPE html> <html> <head> <title><%= title %></title> <link rel='stylesheet' href='/stylesheets/style.css' /> </he
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<% include templates/adminheader.ejs %>
<h1>Respond to Club Requests</h1>
<form name="clubform" method="post">
<% for (var i in clubreq){%>
Club Name:<%= clubreq[i].clubname %><br><br>//I want to access this variable in my app.post() function
Club Type:<%= clubreq[i].type %><br><br>
<input type="submit" value="accept"/><br><br><hr>
<%} %>
</form>
</body>
</html>
让我们从将信息输入表单开始。按如下方式编辑表单:
<form action="/clubreq" method="post">
<% for (var i in clubreq){%>
Club Name: <input type="text" name="clubname[]" value="<%= clubreq[i].clubname %>" /><br><br>
Club Type: <input type="text" name="clubtype[]" value="<%= clubreq[i].type %>" /><br><br>
<input type="submit" value="accept"/><br><br><hr>
<%} %>
</form>
接下来,您可以访问方法中的post数据,如下所示:
app.post('/clubreq', function(req, res, next){
// req.body object has your form values
console.log(req.body.clubname);
console.log(req.body.clubtype);
});
希望俱乐部名称和类型变量以纯文本形式呈现或在文本输入中提供帮助。另外,app.post()代码实际上不是客户端,是吗?是的,我希望这两个变量以纯文本形式呈现。如果您希望在提交表单时访问服务器端的值,则需要将它们放入输入元素中。您可以选择文本类型或隐藏类型,但除非这些值嵌入到有效的表单字段值中,否则app.post()将无法访问它们。
app.use(express.bodyParser());
app.post('/clubreq', function(req, res, next){
// req.body object has your form values
console.log(req.body.clubname);
console.log(req.body.clubtype);
});