【发布时间】:2017-06-13 09:56:48
【问题描述】:
我想将 GET 或 POST(如 AJAX)值传递给我的 php 文件,该文件返回 json 用于自动完成。我完全迷失了,我找不到如何使用 prefetch 方法准备。这是我的代码
HTML/JS
$(document).ready(function() {
var communautes = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('communaute'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: '/plugins/autocomplete/getCommu.php'
});
var peoples = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('people'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: {
url: '/plugins/autocomplete/getPeople.php?query=%QUERY',
cache: false
}
});
$('.multiple-datasets .typeahead').typeahead({
highlight: true
}, {
name: 'Communautes',
display: 'communaute',
source: communautes,
templates: {
header: '<h6>Communautés</h6>'
}
}, {
name: 'People',
display: 'people',
source: peoples,
templates: {
header: '<h6>Personnes</h6>'
}
});
});
<div class="row">
<div class="col-md-4 col-md-offset-3">
<form action="recherche.php" class="search-form">
<div class="form-group has-feedback multiple-datasets">
<input type="text" class="form-control typeahead" name="search" id="search" placeholder="Rechercher" autocomplete="off">
<span class="glyphicon glyphicon-search form-control-feedback"></span>
</div>
</form>
</div>
</div>
</div>
这是我试图获取 $_GET['query'] 的 php 文件
<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/config.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/plugins/LDAP/setLDAP.php';
$ldap=getLDAP();
$dn = $GLOBALS['userGroup'].",".$GLOBALS['domain'];
$filter="(|(".$GLOBALS['nom']."=".$_GET['query']."*)(".$GLOBALS['prenom']."=".$_GET['query']."*))";
$justthese = array($GLOBALS['nom'],$GLOBALS['prenom']);
$sr=ldap_search($ldap, $dn, $filter, $justthese);
$info = ldap_get_entries($ldap, $sr);
if($info['count']!=0){
for ($i=0; $i < $info['count'] ; $i++) {
$tab[]['people']=$info[$i][$GLOBALS['prenom']][0]." ".$info[$i][$GLOBALS['nom']][0];
}
echo json_encode($tab);
}
else echo "Aucune personne trouvée";
?>
【问题讨论】:
-
控制台有什么东西吗?任何 PHP 错误?
-
控制台什么也没有,也没有错误,我测试了PHP文件,效果很好,他只是在等待$_GET['query']
标签: php jquery ajax typeahead.js bloodhound