Typeahead数据库方法不起作用

时间:2013-08-03 11:20:16

标签: php autocomplete bootstrap-typeahead typeahead typeahead.js

这里我尝试打字。它在JavaScript中工作。如果我连接数据库它不工作。请找出问题所在。有任何替代方法可以做到这一点

<html>
<head>
<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css" />
</head>
<body>

<div style="margin: 50px 50px">
<label for="product_search">Product Search: </label>
<input id="product_search" type="text" data-provide="typeahead">
<label for="product_search">Name Search: </label>
<input id="namesearch" type="text" data-provide="typeahead">
<label for="product_search">Test Search: </label>
<input id="search" name="search" type="text" data-provide="typeahead">
</div>

<script src="js/jquery-1.9.1.min.js"></script>
<script src="js/bootstrap-typeahead.js"></script>

<script>
$(document).ready(function($) {
// Workaround for bug in mouse item selection
$.fn.typeahead.Constructor.prototype.blur = function() {
var that = this;
setTimeout(function () { that.hide() }, 250);
};

$('#product_search').typeahead({
source: function(query, process) {
return ["Deluxe Bicycle", "Super Deluxe Trampoline", "Super Duper Scooter"];
}
});

$('#search').typeahead({
    name: 'search',
    remote: '/search.php?query=%QUERY'
});

$('#namesearch').typeahead({
source: function(query, process) {
return ["Ravindran", "Aravinthan", "Dakshesh"];
}
});


})
</script>

</body>
</html>

和我的db search.php在根文件夹

<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) 
{
    die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db('test')) 
{
    exit;
}

$text = mysql_real_escape_string($_GET['query']);

$sql = 'SELECT id,title FROM games WHERE name LIKE "%'. $text . '%" LIMIT 5';

$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result)) 
{
    $posts[] = array($row['id'] => $row['name']);
}
echo json_encode($posts);
mysql_close($link);
?>

1 个答案:

答案 0 :(得分:1)

您正在混合typeahead的两个不同版本的代码语法。

这个是Bootstrap-Typeahead,它是Twitter Bootstrap项目的一部分:

$('#product_search').typeahead({
    source: function(query, process) {
        return ["Deluxe Bicycle", "Super Deluxe Trampoline", "Super Duper Scooter"];
    }
});

由于您已在代码中包含文件bootstrap-typeahead.js,因此它正在运行。

另一个编码为typeahead.js,也来自Twitter,但作为一个独立的项目:

$('#search').typeahead({
    name: 'search',
    remote: '/search.php?query=%QUERY'
});

当然它没有用,因为你没有把它包含在你的项目中。

因此,选择其中一个并将代码迁移到该版本。我个人建议使用typeahead.js

修改

由于你对此非常有经验,我会更进一步帮助你。

首先,您应在代码中加入typeahead.js并删除对bootstrap-typeahead.js的引用。

接下来,在您的JS部分中,修改代码以使用typeahead.js语法:

$(document).ready(function($) {

    $('#product_search').typeahead({
        name: 'products',
        local: ["Deluxe Bicycle", "Super Deluxe Trampoline", "Super Duper Scooter"]
    });

    $('#namesearch').typeahead({
        name: 'name',
        local: ["Ravindran", "Aravinthan", "Dakshesh"]
    });

    $('#search').typeahead({
        name: 'search',
        remote: '/search.php?query=%QUERY'
    });

});

你的PHP代码看起来很不错,但我想补充一下:

$posts = array();

while循环之前。

否则我认为你会好的,其余的将是你的挑战(如果有的话)。请记住,控制台是你的朋友。

希望它有所帮助。