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