Province-city menu linkage, AJAX-Level 3 menu Linkage

Source: Internet
Author: User

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);?>

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.