The JQ extension is as follows:
// City selected
JQuery. fn. cityselect = function (n, s1, s2, s3 ){
If (n! = 3) & (n! = 2) {n = 2 };
If (s1 = "") {s1 = "province "};
If (s2 = "") {s2 = "city "};
If (s3 = "") {s3 = "area "};
If (n = 2) {// second-level linkage
// Alert (s1 + $ ("select [@ name =" + s1 + "] option [@ selected]"). val ());
$ ("Select [@ name =" + s1 + "]"). bind ("change", function (event ){
$. Ajax ({
Type: "GET ",
Url: "/inc/lib/ajax_city.php ",
Data: "n = v2 & cityid =" + $ ("select [@ name =" + s1 + "] option [@ selected]"). val (),
Success: function (opt) {$ ("select [@ name =" + s2 + "]"). empty (). append (opt );}
});
});
}
If (n = 3) {// three-level linkage
$ ("Select [@ name =" + s1 + "]"). bind ("change", function (event ){
$. Ajax ({
Type: "GET ",
Url: "/inc/lib/ajax_city.php ",
Data: "n = v2 & cityid =" + $ ("select [@ name =" + s1 + "] option [@ selected]"). val (),
Success: function (opt ){
$ ("Select [@ name =" + s2 + "]"). empty (). append (opt );
$. Ajax ({
Type: "GET ",
Url: "/inc/lib/ajax_city.php ",
Data: "n = v3 & cityid =" + $ ("select [@ name =" + s2 + "] option [@ selected]"). val (),
Success: function (opt ){
$ ("Select [@ name =" + s3 + "]"). empty (). append (opt );
}
});
}
});
});
$ ("Select [@ name =" + s2 + "]"). bind ("change", function (event ){
$. Ajax ({
Type: "GET ",
Url: "/inc/lib/ajax_city.php ",
Data: "n = v3 & cityid =" + $ ("select [@ name =" + s2 + "] option [@ selected]"). val (),
Success: function (opt) {$ ("select [@ name =" + s3 + "]"). empty (). append (opt );}
});
});
}
};
Next, the functions in PHP are as follows ========
// City linkage script
Function cityselect ($ n = 2, $ s1 = province, $ s2 = city, $ s3 = area, $ v1 = 320000, $ v2 = 320500, $ v3 = 320501 ){
$ Db = new DB_ SQL; // initialize the database
$ Db-> connect (DB_Database, DB_Host, DB_User, DB_Password); // database connection
$ Db-> DB_ SQL ("set names gbk ");
// Link the AJAX script
$ Tmp = "<script type =" text/javascript "> ";
$ Tmp. = $ (document). ready (;
$ Tmp. = "function (){";
$ Tmp. = $ (). cityselect (. $ n ..,. $ s1..,. $ s2.,. $ s3 .);;
$ Tmp. = "";
$ Tmp. = "});";
$ Tmp. = "</script> ";
// Select Province
$ Tmp. = "<select id =" ". $ s1." "name =" ". $ s1." "> ";
$ Db-> query ("select * from sz_chinacity where right (CityPostCode, 4) = 0000 order by CityID asc ");
While ($ db-> next_record ()){
If ($ db-> Record [CityPostCode] ==$ v1 ){
$ Tmp. = "<option value = "". $ db-> Record [CityPostCode]. "" selected> ". $ db-> Record [CityName]. "</option> ";
} Else {
$ Tmp. = "<option value = "". $ db-> Record [CityPostCode]. ""> ". $ db-> Record [CityName]. "</option> ";
}
}
$ Tmp. = "</select> ";
// Select a city
$ Tmp. = "<select id =" ". $ s2." "name =" ". $ s2."> ";
$ Cityid = substr ($ v1, 0, 2 );
$ Db-> query ("select * from sz_chinacity where Right (CityPostCode, 2) = 00 and Left (CityPostCode, 2) = ". $ cityid. "and Right (CityPostCode, 4) <> 0000 order by CityID asc ");
While ($ db-> next_record ()){
If ($ db-> Record [CityPostCode] ==$ v2 ){
$ Tmp. = "<option value = "". $ db-> Record [CityPostCode]. "" selected> ". $ db-> Record [CityName]. "</option> ";
} Else {
$ Tmp. = "<option value = "". $ db-> Record [CityPostCode]. ""> ". $ db-> Record [CityName]. "</option> ";
}
}
$ Tmp. = "</select> ";
If ($ n = 3 ){
// Select Region
$ Tmp. = "<select id =" ". $ s3." "name =" ". $ s3." "> ";
$ Cityid = substr ($ v2, 0, 4 );
$ Db-> query ("select * from sz_chinacity where Left (CityPostCode, 4) = ". $ cityid. "and Right (CityPostCode, 2) <> 00 order by CityID asc ");
While ($ db-> next_record ()){
If ($ db-> Record [CityPostCode] ==$ v3 ){
$ Tmp. = "<option value = "". $ db-> Record [CityPostCode]. "" selected> ". $ db-> Record [CityName]. "</option> ";
} Else {
$ Tmp. = "<option value = "". $ db-> Record [CityPostCode]. ""> ". $ db-> Record [CityName]. "</option> ";
}
}
$ Tmp. = "</select> ";
}
Echo $ tmp;
}
When calling: <? Php cityselect (2);?>