如何解决单击按钮后如何打开新的ejs文件?
我正在创建一种表单,在其中单击“提交”后,我想打开另一个ejs页面。现在我无法执行此操作。我将第二个文件放到app.js中。
app.js
app.get('/form1',function (req,res) {
res.render('form',{
title: "Login",action: "/form1",fields: [
{ name: 'Name',type: 'text',property: 'required' },{ name: 'Email',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页面上单击Submit时,它将自动打开第二个ejs页面或文件2。
,您可以这样做。
app.post('/form1',res) {
//Do wherever you want here like fetching data and show from the previous form.
res.render('form2',{
title: "Show",fields: [
{ name: 'Name',type: 'text',property: 'required' },{ name: 'Email',property: 'required' }
{ name: 'Phone No',type:'text',property: 'required' }
]
} );
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。