weixin_33690963 2015-09-21 18:39 采纳率: 0%
浏览 41

来自node.js的基本警报

I have a form where a user enters their email, password and class. I send the user the HTML file from node.js using:

app.get('/', function (req, res) {
    res.sendFile(path.join(__dirname + '/Login-Signup.html'));
});

I think get the variables from the form using:

var email = req.body.email;
var password = req.body.password;
var cpassword = req.body.cpassword;
var userclass = req.body.userclass;

This is how my HTML form is set out:

<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>

When the values have been entered, the variables must pass a few checks to see if they are valid. They are laid out as so:

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
    }
}

When the email or password is not valid (to my checks) I want an alert box to appear, displaying the problem. I've looked into using AJAX, experimenting with the code but nothing seems to work for me. Is it the way I'm collecting the variables from the HTML? Is it how my get/post commands are laid out?

Thanks in advance.

PS; I am using express.

  • 写回答

1条回答 默认 最新

  • weixin_33686714 2015-09-21 19:03
    关注

    I suggest you use the Ajax (Jquery) post and res.send to get the result in the front-end:

    Front-end:

            <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.
        }
    }
    

    Remove the form and get the values through javascript.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog