我正在使用最新的devbridge自动完成功能,该功能可通过mysql数据库显示德国城市,但不会清除特殊字符。
我的测试文件如下:
<html>
<html lang="de">
<head>
<meta charset="utf-8"/>
<script src="jquery-1.8.2.min.js"></script>
<script src="jquery.autocomplete.min.js"></script>
<link rel="stylesheet"type="text/css" href="styles.css">
<style>
h1 {text-align: center; margin: 20px 20px;}
#autocomplete,#country {margin: 0 auto;}
</style>
</head>
<body>
<h1>Autocomplete Input</h1>
<input type="text" id="autocomplete">
<script>
$('#autocomplete').autocomplete({
serviceUrl: 'search.php',
minChars: 2,
onSelect: function (suggestion) {
alert('You selected: ' + suggestion.value + ', ' + suggestion.data);
}
});
</script>
</body>
</html>
search.php文件如下:
<?php
///
/*** connect to your DB here ***/
include_once "scripts/connect_all.php";
///
//retrieve the search term and strip and clean input
$term = trim(strip_tags($_GET['query']));
//try to make user input safer
$term = mysqli_real_escape_string($myConnection, $term);
//build a query on the database
$sqlCommand = "SELECT gemeinde FROM cities WHERE gemeinde LIKE '%".$term."%'";
//query the database for entries containing the term
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
$reply = array();
$reply['suggestions'] = array();
while ($row = mysqli_fetch_array($query)) {
$gemeinde = $row["gemeinde"];
$reply['suggestions'][]=htmlentities(stripslashes($row['gemeinde']));
}
mysqli_free_result($query);
//format the array into json data
echo json_encode($reply);
?>
运行search.php?query=münch
时会抛出:
{"suggestions":["Münchehofe","Müncheberg, Stadt","Münchenbernsdorf, Stadt","Korntal-Münchingen, Stadt","Münchwald","München, Landeshauptstadt","Grafing b.München, St","Garching b.München, St","Kirchheim b.München","Gdefr. Geb. (Lkr München)","Münchsmünster","Bodenwerder, Münchhausenstadt","Münchweiler an der Alsenz","Glan-Münchweiler","Münchweiler am Klingbach","Münchweiler an der Rodalb","Waldmünchen, St","Münchberg, St","Münchhausen","Münchsteinach","Schwabmünchen, St"]}
但是当将其提供给自动完成功能时,它将抛出带有正确位置的列表,但会将ü
之类的特殊字符转换为ü
;
Münchehofe
Müncheberg
Münchenbernsdorf
如何确保特殊字符保留在自动完成功能的输出中?
答案 0 :(得分:0)
了解了一下,并在下面分享了答案,以备将来参考。删除htmlentities和反斜杠就可以了,所以:
$reply['suggestions'][]=($row['gemeinde']);