Home >Backend Development >PHP Tutorial >Province and city menu linkage, AJAX level 3 menu linkage_PHP tutorial
First look at the extension of JQ 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);}
});
});
}
};
The next function in PHP is 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 database
$db->connect(DB_Database, DB_Host, DB_User, DB_Password); //Database connection
$db->DB_Sql("set names gbk");
//Linked AJAX script
$tmp = "
";
//Province selection
$tmp .= "
";
//City selection
$tmp .= "
";
if ($n==3){
//Region selection
$tmp .= "
";
}
echo $tmp;
}
When calling: