zoukankan      html  css  js  c++  java
  • 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;

    }
    ?>

  • 相关阅读:
    Android studio 安装已经下载好的gradle.zip文件【ubuntu 14.04 LTS环境】
    Python 2.7.6 安装lxml模块[ubuntu14.04 LTS]
    ubuntu14.04 LTS Python IDE专用编辑器PyCharm开发环境搭建
    Git 创建两个“本地分支”协同工作
    关于refs/for/ 和refs/heads/
    Git 多人协作开发的过程
    gerrit_bash_commands.sh
    Ubuntu Eclipse配置Python开发环境
    看看你那张熬完夜的脸
    2016-01-24
  • 原文地址:https://www.cnblogs.com/lin3615/p/3817368.html
Copyright © 2011-2022 走看看