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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)