Home  >  Article  >  Backend Development  >  Ajax method to implement registration function

Ajax method to implement registration function

小云云
小云云Original
2018-01-12 16:30:051715browse

This article mainly introduces the ajax method to implement the registration function and submit data to the backend database to complete the interaction. Interested friends can refer to it. I hope it can help everyone.

1. When we are validating the form, in order to prevent errors from being sent to the server, we usually set it like this:


   $(function(){
    var isusername;(定一个变量)
    var ispwd;
     $('form').submit(function(e){
       if(!isusername || !ispwd){
         e.preventDefault();(阻止事件的默认事件)
      })
   });

1. In nodejs, we can use the following method to (load) jump pages:
load() in JQuery This is to load a page
window.location() This is to jump to a specified page under windows

2. Installation steps and related operations of MongoDB in node.js:
1. Download the installation program. (window If it is an XP system, you can directly copy the installation directory to the specified directory. If you run the installation program on win7 system, you can choose the installation path yourself. (Note that the installation path does not appear in Chinese) The installed MONgoDB defaults to C:\Program Files\MongoDB\Server\3.2\bin
3. Create a directory data in the same directory as the installation, and then create db in the data directory directory and log directory.
4. In the command line, enter cd C:\Program Files\MongoDB\Server\3.2\bin and press Enter, then enter mongo. Enter the mongo.exe operation interface.
5. Start running MongoDB. You can install the MongoDB program into the Windows service through the following command.
Finally open the command line and switch to the bin directory of MongoDB. Run:

mongod.exe --dbpath "c:\data\db" --logpath "c:\data\log\mongodb.log" --install6. In mongo In .exe, we can complete addition, deletion, modification and query: the following introduction is in order
Before this, we can use show dbs to view all databases in the current mongo. If not, use use f30 (first check if there is F30, If not, a database named f30 will be automatically created)
1>Add: db.users.insert({maen:'dd',age:20})
After adding, you may use db.users.find () to see if the addition is successful
2>Delete: db.users.remove({maen:'dd'}) or db.users.remove({}) (This is to delete all data in users)
3>Modify: db.users.update({maen:'dd'},{age:22}) Modify the age of the name dd to 22
4>Find: db.users.find({age:{ $gt:20}) Find data greater than 20

3. We can submit data through BUTTON and jump to the login interface. The code is as follows:



 $("input[type=button]").click(function(e){
 if(!isUsernameValid || !isPwdValid){ //用if语句来判断当用户名或者密码有一个为false时就弹出一个消息框,并提示:请输入正确的信息。
           alert('请输入正确的信息');
           return;  //结束
         }
         $.ajax({       //用ajax来实现不刷新网页的基础上更新数据
           type:"post", //请求方式
           url:"/users/reg", //路径
           data:{
             username:$("input[name=username]").val(),  //获取input中name为username的值
             pwd:$("input[name=pwd]").val() //获取input中name为pwd的值
           },
           success:function(){
             alert("注册成功");
             window.location = "login.html"; //注册成功就跳转到login.html
           }
         });
         
       })

The above code uses ajax to implement the registration function.


Related recommendations:

10 recommended articles about the registration function

Implement the login registration function code (Node.js +Express+MongoDB)

Thinkphp implements SMS verification registration function_php example

The above is the detailed content of Ajax method to implement registration function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn