我在PHP中创建了一些CMS,它使用来自MySQL的数据进行操作 .
在我的CMS中,我有一些输入字段,我希望从MySQL表中获取jQuery 's fancy autocomplete implemented. Basically, the idea is to create jQuery' s数组...
我正在使用PHP 5.3.0,MySQL 5.0.82和Eclipse 3.4.2 . 我在Eclipse中的PHP项目是UTF-8编码的 . 我的CMS页面采用UTF-8字符编码( <meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
) . 网站本身也采用UTF-8编码 . MySQL数据库,所有表都是UTF-8(utf8_general_ci) .
在这一点上,如果我只输入字母(甚至国际和一些奇怪的符号),一切正常,但如果我输入其中一些字符 "
,_ &
, <
或 >
,我会遇到一些严重的问题......
当我输入这样的文本时,我的数据库中的一切看起来都很好,但是当我尝试为jQuery创建一个数组时,它会因为引号而产生错误(我想这里甚至单引号都有问题)...所以我想我应该以某种方式逃脱他们吧?然后我使用PHP的 htmlspecialchars
和jQuery 's array is created correctly. Even when I click inside input field and start typing text, autocomplete shows all those characters correctly. But if I actually select one of the records with those characters, they suddenly appear like html' s转义字符( "
, &
, <
, >
) . 所以我尝试将 htmlspecialchars_decode
应用于相同的输入字段,但它没有't help... Is there a way to display those characters correctly in my input field when I select a record from jQuery'的自动完成?
我找不到任何东西来解决我的问题...请帮忙!
提前致谢!
编辑:这是我为jQuery创建数组的方式( $tags
只是一个简单的数组):
<?php
$t = implode(", ", $tags);
?>
<script>
$(document).ready(function(){
var data_tags = "<?php echo htmlspecialchars($t); ?>".split(" | ");
$("#input_tags").autocomplete(data_tags, { multiple: true, multipleSeparator: ", " });
});
</script>
我知道它可能不是最好的方式,但通常它的工作原理:)
我这样生成一个输入字段:
<?php
function inputField($label, $name, $type, $size, $default=NULL, $misc=NULL){
$printInput = "<tr><td align=\"right\" valign=\"top\">\n";
$printInput .= $label;
$printInput .= "</td><td>\n";
$printInput .= "<input type=\"".$type."\" size=\"".$size."\" name=\"".$name."\" id=\"".$name."\" value=\"".$default."\"> ".$misc."\n";
$printInput .= "</td></tr>\n";
return $printInput;
}
echo inputField("TAGS", "input_tags", "text", 70, $db_tags);
?>
2 回答
试试
json_encode()
,需要PHP 5.2.0或更高版本 .EDIT 您不需要围绕json_encoded值的引号:
请尝试使用
htmlentities();
.