Use Ajax in PHP to do a login page, verify that the user name is available, and invoke the database dynamically

Source: Internet
Author: User

Basic knowledge of 1.ajax

Ajax is a combination of jquery, PHP and other technologies to extend the comprehensive use of technology, not new content. Ajax is also written in the <script> tag.

If the use of Ajax must be 1 processing pages, processing the page just manipulate the database and return the value, the page is AJAX processing.

2. Use Ajax to do the login page denglu.php

<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >$ (document). Ready (function(e) {$ ("#btn"). Click (function(){//Add click events//username and password to button varU = $ ("#uid"). Val ();//take the user name you entered varp = $ ("#pwd"). Val ();//take the input password//tune Ajax$.Ajax ({URL: "Dengluchuli.php",Data: {u:u,p:p},//The second U and P are just variables that can be written at will, and both U and P in dengluchuli.php are the first. Type: "POST",DataType: "TEXT",Success:function(data) {if(Data.Trim() = = "OK")//To add a space to prevent the contents of the space caused by the error.  {window. location.href= "main.php";//JS jump page, to remember.   } Else { Echo("User name or password error");                }                            }                        }); })}); </script> 

Processing page for login dengluchuli.php

<?PHP$uid=$_post["U"]; $pwd=$_post["P"]; include("./dbda.class.php"); $db=NewDbda ();$sql= "Select password from login where username= ' {$uid}’"; $mm=$db->strquery ($sql, 0); if($mm==$pwd&&$pwd!=""){ Echo"OK";} Else { Echo"NO";}

3. Use Ajax to verify that the user name is available testuid.php

<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > $("#uid"). Blur (function(){//Blur indicates that the user name is triggered//taken when the focus is lost varUID = $ ("#uid").Val ();//Tune Ajax$.Ajax ({URL: "Uidchuli.php",Data: {U:uid},type: "POST",DataType: "TEXT",Success:function(data) {if(data>0)                    {                        $("#ts"). HTML ("the account name already exists"); $("#ts"). CSS ("Color", "red"); } Else {                        $("#ts"). HTML ("The account name is available"); $("#ts"). CSS ("Color", "green"));                }                }                        }); }) </script> 

Handling Page uidchuli.php

<?PHP$uid=$_post["U"]; include("./dbda.class.php"); $db=NewDbda ();$sql= "SELECT count (*) from login where Username= ' {$uid}’"; Echo $db->strquery ($sql, 0);

Effect

4. Call the database dynamically, enter keywords in the search box, the contents of which contain keywords automatically appear under the input box.

list.php

<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > $("#name"). KeyUp (function(){ //Take name varn = $ (this).Val ();if(n!= "")    { //Tune AJX$.Ajax ({URL: "Listchuli.php",Data: {n = {},type: "POST",DataType: "TEXT",Success:function(data) {varSZ = data.Split("|"); varstr = "";  for(vari=0;i<sz.length;i++) {str= str+ "<div class= ' l ' >" +sz[i]+ "</div>"; }                $("#list").html (str);        }                    }); } Else {            $("#list"). HTML (""); }        }) </script> 

Handling Page listchuli.php

<?PHP$name=$_post["N"]; include("./dbda.class.php"); $db=NewDbda ();$sql= "Select AreaName from chinastates where areaname like '%{$name}%’"; Echo $db->strquery ($sql, 0);

Effect

Use Ajax in PHP to do a login page, verify that the user name is available, and invoke the database dynamically

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.