Three-level linkage and application of encapsulation

Source: Internet
Author: User

A well-encapsulated JS method

Sanjiliandong.js

$ (document). Ready (function(e) {$ ("#sanji"). HTML ("<select id= ' sheng ' ></select><select id= ' shi ' ></select><select id= ' qu ' ></select> ");    Fillsheng ();    Fillshi ();        Fillqu (); $("#sheng"). Change (function() {Fillshi ();                Fillqu (); })    $("#shi"). Change (function() {fillqu (); })        functionFillsheng () {varCode = "0001"; $.ajax ({async:false,//AsynchronousURL: "chuli.php", Data:{code:code}, type:"POST", datatype:"TEXT", Success:function(data) {varHang = Data.split (' | ')); vars = "";  for(vari=0;i)                    {                        varLie = hang[i].split (' ^ '); S= s+ "<option value= '" +lie[0]+ ">" +lie[1]+ "</option>"; }                                        $("#sheng"). HTML (s);    }                        }); }        functionFillshi () {varCode = $ ("#sheng"). Val (); $.ajax ({async:false,//AsynchronousURL: "chuli.php", Data:{code:code}, type:"POST", datatype:"TEXT", Success:function(data) {varHang = Data.split (' | ')); vars = "";  for(vari=0;i)                    {                        varLie = hang[i].split (' ^ '); S= s+ "<option value= '" +lie[0]+ ">" +lie[1]+ "</option>"; }                                        $("#shi"). HTML (s);    }                        }); }        functionFillqu () {varCode = $ ("#shi"). Val (); $.ajax ({async:false, URL:"Chuli.php", Data:{code:code}, type:"POST", datatype:"TEXT", Success:function(data) {varHang = Data.split (' | ')); vars = "";  for(vari=0;i)                    {                        varLie = hang[i].split (' ^ '); S= s+ "<option value= '" +lie[0]+ ">" +lie[1]+ "</option>"; }                                        $("#qu"). HTML (s);    }                        }); }        });

The page code responsible for processing and connecting to the database :

1. Processing Interface Source code

chuli.php

<? PHP include ("mydbda.php"); $code $_post ["Code"]; $sql = "SELECT * from chinastates where Parentareacode = '". $code. "'" ; $db New Mydbda (); $str $db->select ($sql, "CX", "Enterprise Yellow Pages"); Echo $str ;? >

2. Connect the database code

mydbda.php

<?PHPclassMydbda {var $host= "localhost"; var $username= "Root"; var $password= "123"; var $database= "Enterprise Yellow Pages"; /** Function: Execute SQL statement, return result parameter: $sql: SQL statement to execute $type: type of SQL statement, CX for query, QT for other $data: The database return value to manipulate: if it is a query, returns the result set if it is another statement, the execution returns OK successfully, the failure returns no*/        functionSelect ($sql,$type,$data)        {                        //1. Making Connection Objects            $db=NewMysqli ($this->host,$this->username,$this->password,$data); //2. Determine if the connection is successful            if(Mysqli_connect_error())            {                    Echo"Connection Failed"; //exit the entire program                Exit; }            Else            {                //4. Execute SQL statements                                $result=$db->query ($sql); if($type= = "CX")                {                    $str= "";  while($row=$result-Fetch_row ()) {                         for($i= 0;$i<Count($row);$i++)                        {                            $str=$str.$row[$i]." ^"; }                        $str=substr($str, 0,strlen($str)-1); $str=$str."|"; }                    $str=substr($str, 0,strlen($str)-1); return $str; }                Else                {                    if($result)                    {                        return"OK"; }                    Else                    {                        return"NO"; }                }                                    }        }                }?>

Main Page Source code

sanji.php

<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><Scriptsrc= "Jquery-1.11.2.min.js"></Script><Scriptsrc= "Sanjiliandong.js"></Script><title>Untitled Document</title></Head><Body><Div><DivID= "Sanji"></Div></Div></Body></HTML>

Three-level linkage and application of encapsulation

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.