How to write a login using Ajax

Source: Internet
Author: User
This article describes how to write a login using AJAX

The full name of Ajax is asynchronous JavaScript and XML (asynchronous JavaScript and XML).

Advantages of Ajax:

1, the biggest point is that the page does not refresh, the user experience is very good.
2, the use of asynchronous way to communicate with the server, with more rapid response capability.
3, can transfer the work of some previous server to the client, take advantage of the client idle ability to handle, reduce the burden of server and bandwidth, save space and broadband rental cost. and reduce the burden on the server, the principle of Ajax is "on-demand data", can minimize redundancy requests, and response to the burden on the server.
4, based on standardized and widely supported technology, do not need to download plug-ins or small programs.
5. Ajax can make Internet applications smaller, faster, and more friendly.

Here I use Ajax to write a simple login page: The first is to use the database login table,

Here is the code for the login page, first to introduce the jquery package


Login page is very simple, no, the previous blog has been written many times

And here's the Ajax notation.

<script type= "Text/javascript" >$ ("#btn"). Click (function () {///First step: Fetch data, here the username and password    var uid=$ ("#uid"). Val ( );    var pwd=$ ("#pwd"). Val ();    Second step: Verify the data, here need to tune data from the database, we use the Ajax    $.ajax ({        URL: "dlchuli.php",//Request address        data:{uid:uid,pwd:pwd},//submitted data        type: "POST",//submitted in the way        dataType: "Text",//return type text string JSON XML        success:function (data) {      //start before going to space, Use Trim ()           if (data.trim () = = "OK")           {               window.location.href = "main.php";           }            else{              alert ("User name or password error");}})    }) </script>

dlchuli.php code is written as follows:

<?phpinclude ("DADB.class.php"); $db =new dadb (); $uid =$_post["UID"]; $pwd =$_post["pwd"]; $sql = "Select password from Login where username= ' {$uid} ', $arr = $db->query ($sql), if ($arr [0][0]= $pwd &&!empty ($pwd)) {    echo "OK" ;} else{    echo "NO";}? >

Write here, the simple login page written in Ajax is completed, the biggest advantage is that if the error will be in the original page error, will not jump to other pages

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.