devbridge自动完成中的特殊字符

时间:2018-07-13 09:44:59

标签: php json ajax autocomplete

我正在使用最新的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"]}

但是当将其提供给自动完成功能时,它将抛出带有正确位置的列表,但会将ü之类的特殊字符转换为&uuml;

  • M&uuml;nchehofe
  • M&uuml;ncheberg
  • M&uuml;nchenbernsdorf

如何确保特殊字符保留在自动完成功能的输出中?

1 个答案:

答案 0 :(得分:0)

了解了一下,并在下面分享了答案,以备将来参考。删除htmlentities和反斜杠就可以了,所以:

$reply['suggestions'][]=($row['gemeinde']);