php 商务网站购物车联动地址

数据表如下:

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" id="site_url">
    <div id="box">
                <div class="tr china len2">
                    <span><b></b>省份:</span>
                    <select name="province" class="province option">
                        <option>-请选择省-</option>
                        <?php foreach ($province as $key => $value):?>
                        <option><?php echo $value['region_name'];?></option>
                        <?php endforeach;?>
                    </select>
                    <select name="city"  class="city option">
                        <option>-请选择市-</option>
                    </select>  
                     <select name="county"  class="county option">
                        <option>-请选择区/县-</option>
                    </select>                                     
                </div>
                <div class="address len2">
                    <span><b></b>地址:</span>
                    <textarea name="address"></textarea>
                </div>
</div>
<input type="submit" name="submit" value="submit" />
</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;

}
?>

原文地址:https://www.cnblogs.com/lin3615/p/3817368.html