0
我正在嘗試在窗體內創建一個小標籤輸入,我希望直接從MySQL獲取數據。Twitter Typeahead標籤從MySQL獲取數據
我發現了一個關於製作本地版本的教程,其中數據存儲在頁面中的數組內部,這種方法很好,但我希望它能夠從MySQL中快速地檢索數據。
我試圖修改腳本從本地到遠程,我成功地通過與Json的遠程文件連接到MySQL數據庫 - 我只是無法弄清楚如何使查找與輸入字段一起工作,所以Typeahead(自動完成)對輸入做出反應。
我不是在jQuery的神諭,但我在努力學習:-)
我的代碼如下所示:
tags.php
<!-- Bootstrap styling for Typeahead -->
<link href="/dist/css/tokenfield-typeahead.css" type="text/css" rel="stylesheet">
<link href="/dist/css/bootstrap-tokenfield.css" type="text/css" rel="stylesheet">
<link href="/docs-assets/css/pygments-manni.css" type="text/css" rel="stylesheet">
<link href="/docs-assets/css/docs.css" type="text/css" rel="stylesheet">
<script type="text/javascript" src="/dist/bootstrap-tokenfield.js" charset="UTF-8"></script>
<script type="text/javascript" src="/docs-assets/js/scrollspy.js" charset="UTF-8"></script>
<script type="text/javascript" src="/docs-assets/js/affix.js" charset="UTF-8"></script>
<script type="text/javascript" src="/docs-assets/js/typeahead.bundle.min.js" charset="UTF-8"></script>
<p><strong>Using Twitter Typeahead</strong></p>
<form class="tagsform" method="post" action="/pages/tagscompile.php">
<div class="form-group">
<input type="text" class="form-control tokenfield-typeahead" name="tags" />
</div>
<div class="form-group">
<input type="submit" class="btn btn-default" name="send" value="SEND">
</div>
</form>
<script>
var engine = new Bloodhound({
datumTokenizer: function(d) {
return Bloodhound.tokenizers.whitespace(d.value);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
'cache': false,
url: '/pages/tags_engine.php?q=%QUERY',
wildcard: '%QUERY',
filter: function (data) {
return data;
}
}
});
engine.initialize();
$('.tokenfield-typeahead').tokenfield({
typeahead: [null, { source: engine.ttAdapter() }]
});
</script>
我tags_engine.php文件看起來像這樣:
<?php include($_SERVER['DOCUMENT_ROOT'].'/settings.inc.php');
$q = strtolower($_GET["q"]);
if (!$q) return;
$sql = "select tags as value from tags";
$rsd = mysql_query($sql);
while($rs = mysql_fetch_assoc($rsd)) {
$rows[]=$rs;
}
// I am working on learning MySQLi but until that sticks, this MySQL will do.
print json_encode($rows);
?>
我真的很感激你的幫忙。