doulongdan2264 2015-09-18 21:44
浏览 12
已采纳

PHP选择下拉[关闭]

I am trying to make a form with several drop downs for selecting location in the world.

<div class="form-group control-group">
                    <label for="text" class="col-sm-2 control-label">Country</label>
                    <div class="col-sm-8">
                        <select class="form-control">
                            <option>1</option>
                            <option>2</option>
                            <option>3</option>
                            <option>4</option>
                            <option>5</option>
                        </select>
                    </div>
                </div>

                <div class="form-group control-group">
                    <label for="text" class="col-sm-2 control-label">State/Province</label>
                    <div class="col-sm-8">
                        <select class="form-control">
                            <option>1</option>
                            <option>2</option>
                            <option>3</option>
                            <option>4</option>
                            <option>5</option>
                        </select>
                    </div>
                </div>

                <div class="form-group control-group">
                    <label for="text" class="col-sm-2 control-label">City</label>
                    <div class="col-sm-8">
                        <select class="form-control">
                            <option>1</option>
                            <option>2</option>
                            <option>3</option>
                            <option>4</option>
                            <option>5</option>
                        </select>
                    </div>
                </div>

                <div class="form-group control-group">
                    <label for="text" class="col-sm-2 control-label">Postal Code</label>
                    <div class="col-sm-8">
                        <input type="text" class="form-control" id="subject" name="subject">
                    </div>
                </div>

I want to display only the country list if country is not selected and country and state/province list if country is selected... so on. The list will be stored in SQL.

Any help is appreciated. Thanks.

  • 写回答

1条回答 默认 最新

  • duangai1916 2015-09-18 22:49
    关注
    <select id="country" class="form-control" onchange="javascript:countryChange();">
        <option>1</option>
        <option>2</option>
    </select>
    
    <select id="state" class="form-control" onchange="javascript:stateChange();">
    </select>
    
    <select id="city" class="form-control"></select>
    
    <script>
    function countryChange() {
        var selected = jQuery("#country option:selected").text();
    
        jQuery.ajax({
            url: "country2states.php?country="+selected
        }).done(function(msg) {
            jQuery("#state").html('');
            jQuery("#city").html('');
            jQuery("#state").html(msg);
        });
    }
    
    function stateChange() {
        var selected = jQuery("#state option:selected").text();
    
        jQuery.ajax({
            url: "state2cities.php?state="+selected
        }).done(function(msg) {
            jQuery("#city").html('');
            jQuery("#city").html(msg);
        });
    }
    </script>
    

    For the first function to work, you have to create a PHP page (country2states.php) to handle the database query to retrieve the list of states for the selected country

    "SELECT statename FROM states WHERE country='" . $_GET['country'] . "';"
    

    and return it ECHOing a string formatted like this:

    "<option>Iowa</option><option>South Dakota</option><option>Texas</option>....."
    

    Same stuff for the second function, you will create state2cities.php and you know the rest. :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?