0

这是 html 我的代码

<input type="text" name="country" id="country"/>
<input type="text" name="city" id="city"/>

这是我的jQuery代码

$(document).ready(function() {

$('#country').keydown(function(){
    $(this).autocomplete({
        source: "get_country.php",
        minLength: 0,
        autoFocus: true,
        select: function(event, ui)
        {

        }
    });
});

var country = $("#country").val(); 

$('#city').keydown(function(){
    $(this).autocomplete({
        source: "get_city.php",
        //type:"GET",
        data : 'country='+country,
        minLength: 0,
        autoFocus: true,
        select: function(event, ui)
        {

        }
    });
});

});

这是我的代码 get_city.php

include_once('config.php');

if ( !isset($_REQUEST['term']) )
exit;

$country = $_POST['country'];

$rs = mysql_query('select city from xyz_table where country="$country" and city like "'. mysql_real_escape_string($_REQUEST['term']) .'%" order by city asc limit 0,10');

$data = array();
if ( $rs && mysql_num_rows($rs) )
{
while( $row = mysql_fetch_array($rs, MYSQL_ASSOC) )
{
    $data[] = array(
        'value' => $row['city']
    );
}
}

echo json_encode($data);
flush();

这里显示了以下问题注意:未定义的索引:第6G:\xampp\htdocs\xyz\get_city.php中的国家

我在自动完整搜索中获得了国家/地区,例如德国,英国

我想在国家(例如德国/英国)下获得自动完整搜索城市

我该如何解决它,请任何建议

4

1 回答 1

1

改变这个

$rs = mysql_query('select city from xyz_table where country="$country" and city like "'. mysql_real_escape_string($_REQUEST['term']) .'%" order by city asc limit 0,10');

$rs = mysql_query("select city from xyz_table where country='$country' and city like '". mysql_real_escape_string($_REQUEST['term']) ."%' order by city asc limit 0,10");

查看您的查询$country中的差异设置为字符串而不是实际的country.

用这个

$_REQUEST['country']

使用这个发送数据

data : { country : country },

声明country内部调用

$('#city').keydown(function(){
    var country = $("#country").val();
    $(this).autocomplete({
        source: "get_city.php",
        ............
于 2013-03-30T06:13:41.550 回答