我有这段代码
Function.js文件
$(function() {
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( ".country_suggestion" ).bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).autocomplete( "instance" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
minLength: 1,
source: function( request, response ) {
$.ajax({
type: "POST",
url: "/suggestion.php",
data:'term='+request.term,
success: function(r){
return r;
//response(r);
},
error: function (request, status, error) {
}
});
// delegate back to autocomplete, but extract the last term
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
});
在PHP文件中
<?php
include('inc/safePDO.class.php');
include('inc/config.php');
$dbh = new
SafePDO('mysql:host='.$db['host'].';
dbname='.$db['name'].';charset=UTF8', $db['user'], $db['password']);
$term = $_POST['term'];
$list = $dbh->prepare("SELECT * FROM ((
SELECT `id`, `label` as name, CHAR_LENGTH(`label`) as name_length FROM `country`
WHERE `label` LIKE :name
) UNION (
SELECT `id`, `label` as name, CHAR_LENGTH(`label`) as name_length FROM `countryregion`
WHERE `label` LIKE :name
)) as t
GROUP BY `name`
ORDER BY name_length ASC
LIMIT 0, 10");
$list->execute(array(':name' => "%{$term}%"));
$country_list = $list->fetchAll(PDO::FETCH_OBJ);
$country = array();
foreach($country_list as $country_region_list) {
$country[] = $country_region_list->name;
}
//print_r($country_list);
echo json_encode($country);
我主要通过在谷歌上搜索来尝试,但没有得到结果。
我从数据库中获取数据,如排序形式,但唯一不起作用的是popup .. aucomplete popup没有显示...
如果我在ajax成功打印警报它显示短信数据但在那之后该做什么我不知道...任何帮助将不胜感激。
答案 0 :(得分:0)
PDO不允许多个占位符,请参阅this question
您可以更改代码重命名参数而不重复:
$list = $dbh->prepare("SELECT * FROM ((
SELECT `id`, `label` as name, CHAR_LENGTH(`label`) as name_length FROM `country`
WHERE `label` LIKE :name1
) UNION (
SELECT `id`, `label` as name, CHAR_LENGTH(`label`) as name_length FROM `countryregion`
WHERE `label` LIKE :name2
)) as t
GROUP BY `name`
ORDER BY name_length ASC
LIMIT 0, 10");
$list->execute(array(':name1' => "%{$term}%",':name2' => "%{$term}%"));