我的登录和注册表单都在同一页面上。我已经指定了两种不同的路由来发布数据,但是当我点击注册表单末尾的注册按钮时,它会保存登录表单输入(没有)?我看了这个问题(多种表单,一张处理页面 https://stackoverflow.com/questions/10459012/multiple-forms-and-one-processing-page)。它如何与节点一起工作,这是否意味着我必须为不同的形式创建一个模块?
这是我的代码。我最后还有一个小脚本,因为我的引导模板的滑块功能与提交功能发生冲突。
<h2>Register<h2>
<form method="POST" action="/samples">
<input type="text" name="fullName" id="fullName" class="input-lg " placeholder="your full name" data-required="true">
<input type="text" name="age" id="age" class="input-lg" placeholder="age">
<input type="text" name="city" id="city" class="input-lg" placeholder="Your City">
<input type="text" name="job" class="input-lg" placeholder="Your Job">
<input type="text" name="username" class="input-lg " placeholder="prefered username" data-required="true">
<input type="password" name="password" class="input-lg" placeholder="Password">
<input type="email" name="email" class="input-lg " placeholder="your email" data-required="true">
<button type="submit" class="btn btn-primary">Sign in</button>
</form>
<h2>Login!</h2>
<form method="POST" action="/dashboard">
<hr class="colorgraph">
<input type="email" name="emaillog" class="input-lg " placeholder="your email" data-required="true">
<input type="password" name="passwordlog" class="input-lg" placeholder="Password">
<button type="submit" class="btn btn-primary">Sign in</button>
</form>
<script>
$('button').click( function() {
$('form').submit();
});
</script>
感谢您的帮助
好吧,首先,因为您有两个表单,所以您必须指定单击按钮时要提交哪个表单。
现在,任何按钮单击都只会提交第一个表单。
给出第一个表单的提交按钮类registerButton
和第二个表单的提交类loginButton
。
我还建议对您的表格进行课程。一个将是registerForm
,另一个是loginForm
。
然后更改 HTML 以反映更改
<form method="POST" action="/samples" class="registerForm">
...
<button type="submit" class="btn btn-primary registerButton">Sign in</button>
<form method="POST" action="/dashboard" class="loginForm">
...
<button type="submit" class="btn btn-primary loginButton">Log in</button>
然后将你的 javascript 更改为:
$('button.registerButton').click( function() {
$('form.registerForm').submit();
});
$('button.loginButton').click( function() {
$('form.loginForm').submit();
});
现在您可以将两份表格提交给两个不同的routes.
一份表格将转到samples另一个到仪表板,因此在您的服务器(在 Node 中)上您可以检查它是哪条路由。
我建议使用 Node 框架之一来处理您的应用程序。
你可以试试Express http://expressjs.com/guide.html。它将大大简化您的路线工作。
它将允许您像这样简单地编写路由:
app.post('/samples', function(req, res){
// check register form
// res.send('user' + req.body);
});
app.get('/dashboard', function(req, res){
// check login form
// res.send('user' + req.body);
});
您可以从建议列表中阅读有关 Express 的更多信息here https://stackoverflow.com/a/8149496/2277855.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)