php省市县三级联动

  1. 页面显示代码(position.php)






















    省市区三级联动












  1. 后端接口代码
    (test_citys.php)
connect_error) {
    die("链接失败");
}
$con->query("set names utf8");
$sql = "SELECT * from com_city where province_id = $province_id and in_use = 1;";
$ret = $con->query($sql);
if ($ret->num_rows > 0) {
    while ($row = $ret->fetch_assoc()) {
        $temp = array();
        $temp['id'] = $row['id'];
        $temp['name'] = $row['name'];
        $result[] = $temp;
    }
}
$con->close();
echo json_encode($result);
 ?>

(test.countrys.php)

connect_error) {
    die("链接失败");
}
$con->query("set names utf8");
$sql = "SELECT * from com_district where city_id = $city_id and in_use = 1;";
$ret = $con->query($sql);
if ($ret->num_rows > 0) {
    while ($row = $ret->fetch_assoc()) {
        $temp = array();
        $temp['id'] = $row['id'];
        $temp['name'] = $row['name'];
        $result[] = $temp;
    }
}
$con->close();
echo json_encode($result);
 ?>

(test_provinces.php)

connect_error) {
    die("连接失败 : ".$conn->connect_error);
}
$con->query("set names utf8");
$sql = "SELECT * from com_province where in_use = 1;";
$ret = $con->query($sql);
if ($ret->num_rows > 0) {
    while ($row = $ret->fetch_assoc()) {
        $temp = array();
        $temp['id'] = $row['id'];
        $temp['name'] = $row['name'];
        $result[] = $temp;
    }
}
$con->close();
echo json_encode($result);
 ?>
  1. 数据库
    1.省名称表:字段id、name、in_use(是否启用);
    2.市名称表:字段id、name、province_id、in_use(是否启用);
    3.县名称表:字段id、name、city_id、in_use(是否启用)。

你可能感兴趣的:(php省市县三级联动)