node.js - 单击按钮后如何打开新的ejs文件?
问题描述
我正在创建一个表单,单击提交后我想打开另一个 ejs 页面。现在我无法做到这一点。我将我的第二个文件放在 app.js 中。
应用程序.js
app.get('/form1', function (req, res) {
res.render('form', {
title: "Login",
action: "/form1",
fields: [
{ name: 'Name', type: 'text', property: 'required' },
{ name: 'Email', type: 'text', property: 'required' }
{ name: 'Phone No', type:'text', property: 'required' }
]
});
});
ejs文件如下
form1.ejs
<form action="<%= action %>" method="post">
<% if (fields.length) { %>
<% fields.forEach(function(field) { %>
<label><%= field.name %></label>
<input type="<%= field.type %>" name="<%= field.name %>"
<% if (field.property) { %>
<%= field.property %>
<% } %>
>
<% }) %>
<% } %>
<button type="submit"><%= title %></button> <!-- Title for button is same as that of the page -->
</form>
单击提交按钮后我要打开的第二个文件。
<form action="<%= action %>" method="post">
<h> Data goes here </h>
</form>
解决方案
为form1(第一个ejs页面/表单)创建另一个路由(POST)
app.post('/form1', function (req, res) {
// Do the task with the form data submitted
// Redirect to form2/file 2
res.redirect(URL_to_second_form or file2);
}
现在,当用户在 form1 页面点击提交时,它会自动打开第二个 ejs 页面或文件 2。
推荐阅读
- python - 如何使用 tf-keras-vis 正确实现损失函数?
- python - 删除csv中特定数量的字符
- python - PyInstaller 错误:ModuleNotFoundError:没有名为“statsmodels.__init__._version”的模块
- java - 解析文本文件并有效地拆分它
- javascript - jquery点击输入字段显示div,点击body隐藏div
- javascript - MS 图形 API:带有“/me/onlineMeetings”请求的 400 AuthenticationError
- firebase - 配置为单页应用(将所有网址重写为/index.html)后出现“出现意外错误”?是的
- maven - 将功能 Websphere Liberty 配置文件添加到应用程序
- node.js - 在重定向之前在 Express 应用中设置自定义引用者
- python - Google Calendar API 删除“conferenceData”嵌套对象