数据表如下:

CREATE TABLE IF NOT EXISTS `china` (
`region_id` smallint(5) unsigned NOT NULL,
  `parent_id` smallint(5) unsigned NOT NULL DEFAULT '0',
  `region_name` varchar(120) NOT NULL DEFAULT '',
  `region_type` tinyint(1) NOT NULL DEFAULT '2',
  `agency_id` smallint(5) unsigned NOT NULL DEFAULT '0'
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

 数据见文件:

测试页test.php,

在这个文件得先引入 jquery文件和相应的js文件(accoun_1.js),

用 ajax技术查找(ajax_1.php)

test.php文件如下

<?php
session_start();
$link = mysql_connect('localhost', 'root', '123456') or die("Error: " . mysql_error());
mysql_select_db('trade', $link);
mysql_query("set names utf8", $link);
$province = array();
$sql = "select * from china where parent_id = 1";

$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result))
{
    $province[$row['region_id']] = $row;
}

?>
<script src="jquery-1.10.2.js" type="text/javascript"></script>
<script src="accoun_1.js" type="text/javascript"></script>
<center>
<form name="myform" action="deal.php" method="post">
<input type="hidden" value="ajax_1.php" />
</form>
</center>

acoun_1.js文件如下:

// id = 1为省份,2为城市

$(document).ready(function(){
    var Iprovince=$('#box .china .province');
    var Icity=$('#box .china .city');
    var Icounty=$('#box .china .county');
    var address = $('#box .address textarea');

    Iprovince.change(function(){
        var province = $(this).val();
        address.text(province);
        var _url = 'ajax_1.php';
        $.ajax({
            url:_url,
            type: 'post',
            data:{'prov': province, 'id': 1},
            success:function(data)
            {
              Icity.append(data);
            }
        });
    });    

    Icity.change(function(){
        var city = $(this).val();
        address.text(address.text()+city);
        var _url = 'ajax_1.php';
        $.ajax({
            url:_url,
            type:'post',
            data:{'city':city, 'id': 2},
            success:function(data)
            {
                Icounty.append(data)
            }
        });
        
    });

    Icounty.change(function(){
        var country = $(this).val();
        address.text(address.text()+country);
    })

});

ajax_1.php文件如下:

<?php
session_start();
$link = mysql_connect('localhost', 'root', '123456') or die("Error: " . mysql_error());
mysql_select_db('trade', $link);
mysql_query("set names utf8", $link);
// 1 表省份 2 表城市
if($_POST['id'] == 1)
{
    $region_name = $_POST['prov'];
    $city = '';
    $sql = "select * from china where region_name = '{$region_name}'";

    $result = mysql_query($sql);
    $pro_row = mysql_fetch_assoc($result);
    $pro_id = $pro_row['region_id'];
    
    $query = "select * from china where parent_id = '{$pro_id}'";
    $res = mysql_query($query, $link);
    while($row = mysql_fetch_assoc($res))
    {
        $city .= '<option>' . $row['region_name'] . '</option>';
    }
    echo $city;
    
}else if($_POST['id'] == 2)
{
    $region_name = $_POST['city'];
    $country = '';
    $sql = "select * from china where region_name = '{$region_name}'";

    $result = mysql_query($sql);
    $pro_row = mysql_fetch_assoc($result);
    $pro_id = $pro_row['region_id'];
    
    $query = "select * from china where parent_id = '{$pro_id}'";
    $res = mysql_query($query, $link);
    while($row = mysql_fetch_assoc($res))
    {
        $country .= '<option>' . $row['region_name'] . '</option>';
    }
    echo $country;

}
?>

相关文章:

  • 2021-08-29
  • 2022-12-23
  • 2022-12-23
  • 2021-12-12
  • 2021-12-04
  • 2022-01-25
  • 2021-12-19
猜你喜欢
  • 2021-06-21
  • 2021-07-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-01
  • 2022-02-03
相关资源
相似解决方案