AJAX implements user login registration and exit

Source: Internet
Author: User
Ajax enables users to sign in and exit Step One: Verify the user name

Get
guestbook/index.php
M:index
A:verifyusername
Username: User name to verify
Returns
Code: returned message 0 = no error, 1 = Error
Message: Returned information specifically returned
Step Two: User registration

Post
guestbook/index.php
M:index
A:reg
Username: User name to verify
Password: registration password
Returns
Code: returned message 0 = no error, 1 = Error
Message: Returned information specifically returned
Third step: User Login

Call the update status function when the login is successful
Post
guestbook/index.php
M:index
A:login
Username: User name to log in
Password: login password
Returns
Code: returned message 0 = no error, 1 = Error
Message: Returned information specifically returned
Step Fourth: Verify Cookies

Write a function to get a cookie to write a function that updates the user's State based on whether the cookie value exists Fifth step: User Exit

Call the update status function when the user exits
Post
guestbook/index.php
M:index
A:logout
Returns
Code: returned message 0 = no error, 1 = Error
Message: Returned information specifically returned
(These are step descriptions and some backend interfaces)

The complete code is as follows

Window.onload=function () {var Ousername1=document.getelementbyid (' username1 ');//Username box (registered) var overifyusernamemsg= document.getElementById (' verifyusernamemsg '); Validate User name prompt box (register) var Opassword1=document.getelementbyid (' Password1 ' )//Password box (registered) var Obtnreg=document.getelementbyid (' Btnreg ');//Submit button (register) var Ousername2=document.getelementbyid (' Use Rname2 ');//Username box (login) var Opassword2=document.getelementbyid (' Password2 ');//Password box (login) var obtnlogin=document.getelemen Tbyid (' Btnlogin ');//Submit button (login) var Oreg=document.getelementbyid (' Reg ');//Register box var Ologin=document.getelementbyid (' Lo Gin ');//Login box var ouser=document.getelementbyid (' user ');//Exit box var Ouserinfo=document.getelementbyid (' userinfo ');//user
            Name (exit box) var Ologout=document.getelementbyid (' logout ');/Exit button (Exit box) function GetCookie (key) {//Get cookie value var arr=document.cookie.split (';
            ');
            Console.log (arr); for (Var i=0;i<arr.length;i++) {var Arr2=arr[I].split (' = ');
                Console.log (ARR2);
                if (Arr2[0]==key) {return arr2[1];
    }
            }
        };

    Updatauser ();
        function Updatauser (ouid) {//Update state function var ouid=getcookie (' uid ');

        var Ousername=getcookie (' username ');
            if (ouid) {ouser.style.display= ' block ';
            Ouserinfo.innerhtml=ousername;
            Oreg.style.display= ' None ';
        Ologin.style.display= ' None ';
            }else{ouser.style.display= ' None ';
            Ouserinfo.innerhtml= "";
            oreg.style.display= ' block ';
        ologin.style.display= ' block '; } ousername1.onblur=function () {//user name Validation ajax (' Get ', ' guestbook/index.php ', ' M=index&a=verifyuserna

             Me&username= ' +this.value,function (data) {var info=json.parse (data);
             Overifyusernamemsg.innerhtml=info.message; if (Info.code) {Overifyusernamemsg.style.Color= ' Red ';
             }else{overifyusernamemsg.style.color= ' green ';
    }
        });
    }; Obtnreg.onclick=function () {//Register submit button Ajax (' Post ', ' guestbook/index.php ', ' M=index&a=reg&use Rname= ' +encodeuri (ousername1.value) + ' &password= ' +opassword1.value, function (data) {var info=js
            On.parse (data);
            alert (info.message);
    if (!info.code) {oreg.style.display= ' None '}});
    }; Obtnlogin.onclick=function () {//Login submit button Ajax (' Post ', ' guestbook/index.php ', ' m=index&a=login&
                Amp;username= ' +encodeuri (ousername2.value) + ' &password= ' +opassword2.value, function (data) {
                var info=json.parse (data);
                alert (info.message);
                if (! Info.code) {updatauser ();

    }

            });

           };
   Ologout.onclick=function () {//Exit Submit button         Ajax (' Post ', ' guestbook/index.php ', ' m=index&a=logout ', function (data) {var inf
                O=json.parse (data);
                alert (info.message);
                if (! Info.code) {updatauser ();
            }

            });

           return false; }

}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.