devbridge autocomplete查询AJAX实现

时间:2016-01-24 10:35:53

标签: ajax autocomplete

我的格式devbridge autocomplete遇到了麻烦

我的标题部分:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="/autocomplete/jquery.autocomplete.min.js"></script>
输入字段的

页面如下所示:

<script>
$('#client_recommendation').autocomplete({
    serviceUrl: '/autocomplete/ClientRecommendation.php',
    onSelect: function (suggestion) {
        alert('You selected: ' + suggestion.value + ', ' + suggestion.data);
    }
});
</script>

<form action="/scripts/add_client.php" method="post" name="add_client">
    <fieldset>
        <label for="client_recommendation">Recommendation:</label>
        <input name="client_recommendation" size="25" type="text" id="client_recommendation" />
    </fieldset>
</form>

ClientRecommendation.php

require '../scripts/connect.php';

$cr  = trim($_GET['client_recommendation']); //also tried trim($_GET['query']);


    $sql = "SELECT  client_name, client_id
                          FROM (
                            SELECT CONCAT(
                                client_name, ' ', client_surname, IF(
                                    LENGTH(client_prompt), 
                                    CONCAT(' (',client_prompt,')'), '')
                                )
                            AS client_name, client_id
                            FROM `clients`
                          ) 
                          clients 
                          WHERE client_name LIKE '%" . $cr . "%' 
                          ORDER BY client_name";

    $result = $conn->query($sql); 
    $reply = array();
    $reply['query'] = $cr;
    $reply['suggestions'] = array();

    while($row = $result->fetch_assoc()) {

        $reply['suggestions'][]=$row['client_name'];

    }

    echo json_encode($reply);

    $conn->close();

作为测试,我尝试了这个:

$cr  = "pa";

并且ClientRecommendation.php会返回正确的结果:

{"query":"pa","suggestions":["Pavlo hryhorash"]}

这是有效的JSON,但在我的表格中,我什么都没得到。 任何帮助将不胜感激

更新
我在页面脚本中添加了$(document).ready,其中包含输入字段:

<script>
    $(document).ready(function(){
    $('#client_recommendation').autocomplete({
        serviceUrl: '/autocomplete/ClientRecommendation.php',
        onSelect: function (suggestion) {
            alert('You selected: ' + suggestion.value);
        }
    });
    });
</script>  

之后生成的建议HTML标记开始加载页面,在输入字段中键入时,我在控制台中看到此错误:

Uncaught SyntaxError: Unexpected token <
m.parseJSON @ jquery.min.js:4g.transformResult @ 
jquery.autocomplete.min.js:49(anonymous function) @  
jquery.autocomplete.min.js:288j @ jquery.min.js:2k.fireWith @ jquery.min.js:2x @ 
jquery.min.js:4b @ jquery.min.js:4

1 个答案:

答案 0 :(得分:0)

问题相当愚蠢,但也许对某人有帮助 在我的if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "<!--Connected successfully !-->"; 中有这个字符串:

echo

此{{1}}阻止了该脚本。我刚评论过,现在一切都很好