Can';t在PHP中构建自动完成文本框

Can';t在PHP中构建自动完成文本框,php,jquery,mysql,autocomplete,Php,Jquery,Mysql,Autocomplete,我一直在学习如何构建一个自动完成的文本框,从我的数据库中获取城市,我使用XAMPP,所以它是mySQL。以下是我从教程中获得并根据需要修改的代码: demo.html <html> <head> <title>Autocomplete demo</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css

我一直在学习如何构建一个自动完成的文本框,从我的数据库中获取城市,我使用XAMPP,所以它是mySQL。以下是我从教程中获得并根据需要修改的代码:

demo.html

    <html>
<head>
<title>Autocomplete demo</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<script>
$(function() {
    $( "#city" ).autocomplete({
        source: 'search.php'
    });
});
</script>   

</head>

<body>
<div class="ui-widget">
    <label for="city">Region: </label>
    <input id="city">
</div>


</body>


</html>

自动完成演示
$(函数(){
$(“#城市”).autocomplete({
来源:“search.php”
});
});
地区:
search.php

<?php
    //database configuration
    $dbHost = 'localhost';
    $dbUsername = 'root';
    $dbPassword = '';
    $dbName = 'sample_master';

    //connect with the database
    $db = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);

    //get search term
    $searchTerm = $_GET['city'];

    //get matched data from skills table
    //"region" is the column from the table I wish to fetch
    $query = $db->query("SELECT * FROM cities WHERE region LIKE '%".$searchTerm."%' ORDER BY region");
    while ($row = $query->fetch_assoc()) {
        $data[] = $row['region'];
    }

    //return json data
    echo json_encode($data);
?>

自动完成默认的$\u-GET为$\u-GET['term'],您被称为$\u-GET['city'],因此您需要更改您的$\u-GET名称,或者您可以像下面那样定义您的GET名称

$(function() {
    $( "#city" ).autocomplete({
        source: function(r,res){
        $.get('search.php',{city:r.term},res,'json');//r is input typing request,res is response result as json
        }
    });
});

在浏览器检查器中检查网络流量。XHR请求是否触发search.php以及响应是什么?@AronCederholm在我键入时确实会触发对search.php的查询,但它们显示取消状态。它似乎在内部工作,我检查了XHR,它给出了200 OK状态(在返回“取消”状态之前)。但是文本框上没有显示任何内容。我添加了上面的代码,并保留了$_GET['city'],尝试将其更改为$_GET['term'],在HTML函数调用中也没有任何更改。没有结果。