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