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

Php 使用组合框列出城市和行政区,php,sql,ajax,combobox,Php,Sql,Ajax,Combobox,我希望用户使用4个组合框来选择他们在哪里(城市和自治区)以及他们想去哪里(城市和自治区)。正在数据库中选择这些选项。 2个“我在哪里”组合框工作正常,但“我想去哪里”组合框工作不正常。不列出属于所选城市的行政区 <body> <?php $con = mysql_connect( 'localhost', 'root', '' ) ; mysql_select_db( 'fretadoaqui', $con ); ?>

我希望用户使用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>

我在哪里
城市: 行政区/地区: 加载。。。 选择一个城市 load('jquery','1.3'); $(函数(){ $('cod_cities')。更改(函数(){ if($(this.val()){ $('cod#u区')。隐藏(); $('.carregando').show(); $.getJSON('boroughs.ajax.php?search=',{cod_cities:$(this.val(),ajax:'true'),函数(j){ var选项=“”; 对于(变量i=0;i我想去哪里
城市: 行政区/地区: 加载。。。 选择一个城市 load('jquery','1.3'); $(函数(){ $('cod#u cities2')。更改(函数(){ if($(this.val()){ $('cod#u区2')。隐藏(); $('.loading').show(); $.getJSON('boroughsgo.ajax.php?search=',{cod_cities2:$(this.val(),ajax:'true'),函数(l){ var选项=“”; 对于(变量i=0;j
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 ) );
<?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 ) );
$row['cod_行政区],
'name'=>(utf8_编码($row['name']),
);
}
echo(json_编码($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_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 ) );
<?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 ) );
$row['cod_行政区2'],
'name'=>(utf8_编码($row['name']),
);
}
echo(json_编码($boroughs));
正在发生的事情:

数据库: