Code for Three-Level ajax linkage implementation,
The examples in this article share the code of the three-level interaction demonstration of ajax for your reference. The details are as follows:
1. test. php
<Script src = ".. /jquery-1.11.2.min.js "> </script> <script src =" sanji. js "> </script> <title> untitled document </title>
2. sanji. js
// JavaScript Document $ (document ). ready (function (e) {// find the DIV with ID = SANJI, create three drop-down items and drop them into $ ("# sanji" ).html ("<select id = 'sheng'> </select> <select id = 'shi'> </select> <select id = 'qu'> </select> "); // load provincial data LoadSheng (); // load Municipal Data LoadShi (); // load area data LoadQu (); // Add an event to the provincial drop-down list $ ("# sheng "). click (function () {// reload the LoadShi (); // reload the LoadQu ();}) // Add an event $ ("# shi") to the drop-down list of the city "). click (function () {// reload LoadQu () ;}); // function LoadSheng () {// province parent code var pcode = "0001"; $. ajax ({async: false, url: "chuli. php ", data: {pcode: pcode}, type:" POST ", dataType:" TEXT ", success: function (data) {var hang = data. trim (). split ("|"); var str = ""; for (var I = 0; I
3. chuli. php
<? Php // give a parent code and return require "DBDA for all sub-regions under the parent code. class. php "; $ db = new DBDA (); $ pcode = $ _ POST [" pcode "]; $ SQL =" select areacode, areaname from chinastates where parentareacode = '{$ pcode}' "; echo $ db-> StrQuery ($ SQL );
Returns a string in the referenced encapsulation class.
<? Phpclass DBDA {public $ host = "localhost"; public $ uid = "root"; public $ pwd = "123"; public $ dbname = "test_123 "; // execute the SQL statement and return the corresponding result // $ SQL statement to be executed // $ type indicates the type of the SQL statement, and 0 indicates addition, deletion, and modification, 1 indicates querying function query ($ SQL, $ type = 0) {$ db = new MySQLi ($ this-> host, $ this-> uid, $ this-> pwd, $ this-> dbname); $ result = $ db-> query ($ SQL); if ($ type) {// if it is a query, show data return $ result-> fetch_all ();} else {// if it is added, return true or false return $ result ;}} // return the string's method public function strquery ($ SQL, $ type = 1) {$ db = new MySQLi ($ this-> host, $ this-> uid, $ this-> pwd, $ this-> dbname); $ result = $ db-> query ($ SQL); $ arr = $ result-> fetch_all (); $ str = ""; foreach ($ arr as $ v) {$ str = $ str. implode ("^", $ v ). "|" ;}$ str = substr ($ str, 0, strlen ($ str)-1); return $ str ;}// return the JSON function JSONQuery ($ SQL, $ type = 1) {$ db = new MySQLi ($ this-> host, $ this-> uid, $ this-> pwd, $ this-> dbname ); $ r = $ db-> query ($ SQL); if ($ type = 1) {return json_encode ($ r-> fetch_all (MYSQLI_ASSOC ));} else {return $ r ;}}}
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.