我正在使用带有远程源的jQuery自动完成小部件。小部件工作正常,除非条目有&符号(&)。我的php页面正在创建的JSON正在使用单个&amp ;;正确显示数据。符号。但是小部件将其解释为html &
。
小部件中的html &
然后导致我的php表单中断。任何帮助将不胜感激。我尝试了几件事似乎没什么用。
这是jQuery:
<script>
$(function() {
function log( message ) {
$( "<div>" ).text( message ).prependTo( "#log" );
$( "#log" ).scrollTop( 0 );
}
$( "#company" ).autocomplete({
source: "autoComp/cds.php",
minLength: 2,//search after two characters
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );
}
});
});
</script>
我的php源页面:
<?php
$mysql = new mysqli("localhost", "root", "root", "casting2", 3306);
// If the connection didn't work out, there will be a connect_errno property on the $mysql object. End
// the script with a fancy message.
if ($mysql->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysql->connect_error . ")";
}//connect to your database
$term = $_GET['term'];//retrieve the search term that autocomplete sends
$theQuery = "SELECT company AS value, company AS id FROM cds WHERE company LIKE '%".$term."%'";
$result = $mysql->query($theQuery);
unset($row_set);
// Move to row number $i in the result set.
for ($i = 0; $i < $result->num_rows; $i++) {
// Move to row number $i in the result set.
$result->data_seek($i);
// Get all the columns for the current row as an associative array -- we named it $aRow
$aRow = $result->fetch_assoc();
$aRow['value'] = htmlentities(stripslashes($aRow['value']));
$aRow['id'] = htmlentities(stripslashes($aRow['id']));
$row_set[] = $aRow; //build an array
}
echo json_encode($row_set);//format the array into json data
$result->free();
?>
这是一个来自数组的条目示例:
[{"value":"Bowling & Miscia Casting","id":"Bowling & Miscia Casting"}]
谢谢!