Ajax write logon and ajax Logon
The full name of AJAX is Asynchronous JavaScript and XML (Asynchronous JavaScript and XML ).
Advantages of ajax:
1. The biggest difference is that the page is refreshing and the user experience is very good.
2. Use asynchronous mode to communicate with the server, providing more rapid response capabilities ..
3. Some of the previous server workload can be transferred to the client, and the client's idle capabilities can be used to handle the workload, reduce the load on servers and bandwidth, and save space and bandwidth rental costs. In addition, it reduces the burden on the server. The ajax principle is to "retrieve data on demand", which can minimize the burden on the server caused by redundant requests and responses.
4. Based on standardized and widely supported technologies, you do not need to download plug-ins or applets.
5. ajax can make Internet applications smaller, faster, and more friendly.
Here I use ajax to write a simple login page: The first thing I need is the database login table,
The following is the logon Page code. First, introduce the jquery package.
1
The login page is very simple, so you won't be able to log on to it. I 've written this blog many times.
Then the following is how ajax is written:
1 <script type = "text/javascript"> 2 $ ("# btn "). click (function () {3 // Step 1: retrieve data. Here the user name and password are used. 4 var uid =$ ("# uid "). val (); 5 var pwd =$ ("# pwd "). val (); 6 // Step 2: verify the data. Here we need to call the data from the database, and we use ajax 7 $. ajax ({8 url: "dlchuli. php ", // request address 9 data: {uid: uid, pwd: pwd}, // submit data 10 type:" POST ", // submit method 11 dataType: "TEXT", // return type TEXT string JSON XML12 success: function (data) {13 // before the start, use trim () 14 if (data. trim () = "OK") 15 {16 window. location. href = "main. php "; 17} 18 else {19 alert (" Incorrect username or password "); 20} 21} 22}) 23 24}) 25 </script>
The code for dlchuli. php is as follows:
1 <?php 2 include("DADB.class.php"); 3 $db=new DADB(); 4 $uid=$_POST["uid"]; 5 $pwd=$_POST["pwd"]; 6 $sql="select password from login where username='{$uid}'"; 7 8 $arr=$db->Query($sql); 9 10 if($arr[0][0]=$pwd && !empty($pwd))11 {12 echo"OK";13 }14 else{15 echo"NO";16 }17 ?>
Here, you can use ajax to write a simple login page. The biggest benefit is that if an error occurs on the original page, it will not be reported to other pages.