使用组合框列出城市和行政区

时间:2015-04-14 15:07:07

标签: php sql ajax combobox

我希望用户使用4个组合框来选择他们所在的位置(城市和自治市)以及他们想去的地方(城市和自治市镇)。正在数据库中选择选项。 2“我在哪里”的组合框工作正常,但“我想去的地方”却没有。未列出属于所选城市的行政区。

    <body>
    <?php
        $con = mysql_connect( 'localhost', 'root', '' ) ;
        mysql_select_db( 'fretadoaqui', $con );
    ?>

Where I am</br>
    <label for="cod_cities">City:</label>
    <select name="cod_cities" id="cod_cities">
        <option value=""></option>
        <?php
            $sql = "SELECT cod_cities, name
                    FROM cities
                    ORDER BY name";
            $res = mysql_query( $sql );
            while ( $row = mysql_fetch_assoc( $res ) ) {
                echo '<option value="'.$row['cod_cities'].'">'.(utf8_encode($row['name'])).'</option>';
            }
        ?>
    </select>


    <label for="cod_boroughs">Borough/region:</label>
    <span class="loading">Loading...</span>
    <select name="cod_boroughs" id="cod_boroughs">
        <option value="">Choose a city</option>
    </select>

    <script src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load('jquery', '1.3');
    </script>

    <script type="text/javascript">
    $(function(){
        $('#cod_cities').change(function(){
            if( $(this).val() ) {
                $('#cod_boroughs').hide();
                $('.carregando').show();
                $.getJSON('boroughs.ajax.php?search=',{cod_cities: $(this).val(), ajax: 'true'}, function(j){
                    var options = '<option value=""></option>';
                    for (var i = 0; i < j.length; i++) {
                        options += '<option value="' + j[i].cod_boroughs + '">' + j[i].name + '</option>';
                    }
                    $('#cod_boroughs').html(options).show();
                    $('.loading').hide();
                });
            } else {
                $('#cod_boroughs').html('<option value="">Choose a city</option>');
            }
        });
    });
    </script>

</br>Where I want to go</br>
    <label for="cod_cities2">City:</label>
    <select name="cod_cities2" id="cod_cities2">
        <option value=""></option>
        <?php
            $sql = "SELECT cod_cities, name
                    FROM cities
                    ORDER BY name";
            $res = mysql_query( $sql );
            while ( $row = mysql_fetch_assoc( $res ) ) {
                echo '<option value="'.$row['cod_cities2'].'">'.(utf8_encode($row['name'])).'</option>';
            }
        ?>
    </select>

    <label for="cod_boroughs2">Borough/region:</label>
    <span class="loading">Loading...</span>
    <select name="cod_boroughs2" id="cod_boroughs2">
        <option value="">Choose a city</option>
    </select>

    <script src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load('jquery', '1.3');
    </script>

    <script type="text/javascript">
    $(function(){
        $('#cod_cities2').change(function(){
            if( $(this).val() ) {
                $('#cod_boroughs2').hide();
                $('.loading').show();
                $.getJSON('boroughsgo.ajax.php?search=',{cod_cities2: $(this).val(), ajax: 'true'}, function(l){
                    var options = '<option value=""></option>';
                    for (var i = 0; j < l.length; i++) {
                        options += '<option value="' + j[i].cod_boroughs2 + '">' + j[i].name + '</option>';
                    }
                    $('#cod_boroughs2').html(options).show();
                    $('.loading').hide();
                });
} else {
                $('#cod_boroughs2').html('<option value="">Choose a city</option>');
            }
        });
    });
    </script>

</body>

bairros.ajax.php 文件

<?php
header( 'Cache-Control: no-cache' );
header( 'Content-type: application/xml; charset="utf-8"', true );

$con = mysql_connect( 'localhost', 'root', '' ) ;
mysql_select_db( 'fretadoaqui', $con );

$cod_cities = mysql_real_escape_string( $_REQUEST['cod_cities'] );
$boroughs = array();

$sql = "SELECT cod_boroughs, name
        FROM boroughs
        WHERE cities_cod_cities=$cod_cities
        ORDER BY name";
$res = mysql_query( $sql );
while ( $row = mysql_fetch_assoc( $res ) ) {
    $boroughs[] = array(
        'cod_boroughs'  => $row['cod_boroughs'],
        'name'          => (utf8_encode($row['name'])),
    );
}

echo( json_encode( $boroughs ) );

bairrosgo.ajax.php 文件

<?php
header( 'Cache-Control: no-cache' );
header( 'Content-type: application/xml; charset="utf-8"', true );

$con = mysql_connect( 'localhost', 'root', '' ) ;
mysql_select_db( 'fretadoaqui', $con );

$cod_cities2 = mysql_real_escape_string( $_REQUEST['cod_cities2'] );

$boroughs = array();

$sql = "SELECT cod_boroughs, name
        FROM boroughs
        WHERE cities_cod_cities=$cod_cities2
        ORDER BY name";
$res = mysql_query( $sql );
while ( $row = mysql_fetch_assoc( $res ) ) {
    $boroughs[] = array(
        'cod_boroughs2' => $row['cod_boroughs2'],
        'name'          => (utf8_encode($row['name'])),
    );
}
echo( json_encode( $boroughs ) );

发生了什么:http://i.stack.imgur.com/pq8ta.png

数据库:http://i.stack.imgur.com/evQeL.png

0 个答案:

没有答案