Ajax and encapsulation into class

Source: Internet
Author: User
Tags erro

<! 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(){                varUID = $ ("#uid").Val (); varPWD = $ ("#pwd").Val (); $.Ajax ({URL: "Chuli.php",//process the path of the pagedata:{u:uid,p:pwd,type:0},//the data passedType: "POST",//How data is submitted for deliveryDataType: "TEXT",//type of return value, text,json,xml three types selectableSuccessfunction(data) {//callback function//check if error display function$ ("#erro"). HTML (Date); if(data== "OK") {window. location = "main.php"; }                Else{alert (data);                }                                }                        }); })                //determine if the user name is available$ ("#user"). Blur (function(){                        varUID = $ (this).Val (); $.Ajax ({URL: "Chuli.php",Data: {u:uid,type:1},type: "POST",DataType: "TEXT",Success:function(d) {if(d== "OK")                    {                        $("#ts"). HTML ("User name available"); }                    Else                    {                        $("#ts"). HTML ("<span style= ' color:red ' > the username already exists! </span> ");                        }                                        }                                }); })            });</script>
<! 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(){                $.Ajax ({URL: "Chuli.php",Data: {Type:2},type: "POST",DataType: "TEXT",Success:function(data) {varHang = data.Split("|"); varstr = "";  for(vari=0;i)                {                    varLie = Hang[i].Split("^"); STR= str+ "<option value= '" +lie[0]+ ">" +lie[1]+ "</option>"; }                                $("#sel").html (str);                }                        }); })        });</script>
<?PHP$type=$_post["Type"];include("dbda.php");$db=NewDbda ();Switch($type){     Case0:$uid=$_post["U"]; $pwd=$_post["P"]; $sql= "SELECT count (*) from login where Username= ' {$uid} ' and password= ' {$pwd}‘"; $attr=$db->query ($sql); if($attr[0] [0]==0)        {            Echo"User name password is not correct!" "; }        Else        {            Echo"OK"; }         Break;  Case1:$uid=$_post["U"]; $sql= "SELECT count (*) from login where Username= ' {$uid}‘"; $attr=$db->query ($sql); if($attr[0] [0] = = 0)        {            Echo"OK"; }        Else        {            Echo"NO"; }                 Break;  Case2:$sql= "SELECT * FROM Nation"; $attr=$db->query ($sql); //n001^ han |n002^ hui |n003^ Miao        $str="";  for($i= 0;$i<Count($attr);$i++)        {                         for($j= 0;$j<Count($attr[$i]);$j++)            {                $str=$str.$attr[$i][$j]; $str=$str." ^"; }            $str=substr($str, 0,strlen($str)-1); $str=$str."|"; }        $str=substr($str, 0,strlen($str)-1); Echo $str;  Break; }

Encapsulation class

<?PHPclassajax{//The method of the Ajax call//sql is the statement to execute//$type is the type of SQL statement, 0 for adding and removing, 1 for querying//$db representing the data to be manipulated     Public functionAjax ($sql,$type=1,$db= "Testa")         {        //Connecting Objects        $conn=NewMysqli ($this->host,$this->uid,$this->pwd,$db); //determine if the connection is successful!Mysqli_connect_error() or die("Connection Failed"); //Execute SQL statement        $result=$conn->query ($sql); $attr=$result-Fetch_all (); //to spell an array into a string                        $str= ""; if($type==1){             for($i= 0;$i<Count($attr);$i++)            {                     for($j= 0;$j<Count($attr[$i]);$j++)                {                    $str=$str.$attr[$i][$j]; $str=$str." ^"; }                $str=substr($str, 0,strlen($str)-1); $str=$str."|"; }                $str=substr($str, 0,strlen($str)-1); return $str." <br> "; }            Else            {                return $result; }    }            }

Ajax and encapsulation into class

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.