我有一个用户输入电子邮件,密码和课程的表单。我使用:
从node.js向用户发送HTML文件app.get('/', function (req, res) {
res.sendFile(path.join(__dirname + '/Login-Signup.html'));
});
我认为使用以下方法从表单中获取变量:
var email = req.body.email;
var password = req.body.password;
var cpassword = req.body.cpassword;
var userclass = req.body.userclass;
这就是我的HTML表单的设置方式:
<form method="post">
<fieldset>
<div class="formTitle">Account Sign Up</div>
<label for="email">Email:</label>
<input type="email" id="email" name="email" placeholder="Enter your email" />
<label for="password">Password:</label>
<input type="password" id="password" name="password" placeholder="Enter your password" />
<label for="cpassword">Confirm Password:</label>
<input type="password" id= "cpassword" name="cpassword" placeholder="Confirm password"/>
<label for="class">Class:</label>
<input id="text" id= "class" name="userclass" placeholder="Enter your class"/>
<input type="submit" formmethod="post" formaction="/" value="Submit Details">
</fieldset>
</form>
输入值后,变量必须通过几项检查才能看出它们是否有效。它们的布局如下:
app.post('/', function(req, res){
function emailCheck(email){
//when theres an error: console.log(error), and return false
}
function passCheck(password){
//when theres an error: console.log(error), and return false
}
if (passCheck == true && emailCheck == true){
//enter user into database
}
}
当电子邮件或密码无效时(我的支票)我想要一个警告框出现,显示问题。我已经研究过使用AJAX,试验代码但似乎没有什么对我有用。这是我从HTML中收集变量的方式吗?是我的get / post命令是如何布局的?
提前致谢。
PS;我正在使用快递。
答案 0 :(得分:0)
我建议你使用Ajax(Jquery)帖子和res.send
来获得前端的结果:
<强>前端强>
<fieldset>
<div class="formTitle">Account Sign Up</div>
<label for="email">Email:</label>
<input type="email" id="email" name="email" placeholder="Enter your email" />
<label for="password">Password:</label>
<input type="password" id="password" name="password" placeholder="Enter your password" />
<label for="cpassword">Confirm Password:</label>
<input type="password" id= "cpassword" name="cpassword" placeholder="Confirm password"/>
<label for="class">Class:</label>
<input id="text" id= "class" name="userclass" placeholder="Enter your class"/>
<input type="button" onclick="save();" value="Submit Details">
</fieldset>
JQuery Ajax:
function save(){
var data = {};
data.email = document.getElementById("email");
data.password = document.getElementById("password");
$.ajax({
type: 'POST',
data: JSON.stringify(data),
cache: false,
contentType: 'application/json',
datatype: "json",
url: '/',
success: function(returns){
if (returns)
alert("User save");
else
alert("Error: user not save");
}
});
<强> Node.js的:强>
app.post('/', function(req, res){
function emailCheck(email){
//when theres an error: console.log(error), and return false
}
function passCheck(password){
//when theres an error: console.log(error), and return false
}
if (passCheck == true && emailCheck == true){
res.send(true); // if ok. You can get the res.send in the success ajax event.
}
}
删除form
并通过javascript获取值。