autocomplete 中文顯示問題
英文,數字,英文開頭+中文,都沒有問題,但是中文的時候就有問題,沒有反映,我用的是php+mysql。
前端網頁:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5"/>
<title>autocomplete demo</title>
</head>
<body>
<label for="t1">請輸入姓名:</label>
<input type="text" name="t1" id="t2" value="請輸入" /><br><br>
<label for="t1">請輸入姓名:</label>
<input type="text" name="t3" id="t4" value="請輸入" />
<link rel="stylesheet" type="text/css" href="js/jquery.autocomplete.css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.autocomplete.pack.js"></script>
<script type="text/javascript" charset= "big5">
$("#t2").autocomplete('autocomplete.php');
$("#t4").autocomplete('autocomplete2.php');
</script>
</body>
</html>
連去db:
<?php
include("db.func");
$sql_thrName = "select cname from thr";
$rows_thrName = GetRows("teacherdata", "$sql_thrName");
$num_thrName = mysql_num_rows($rows_thrName);
while (List($cname)= mysql_fetch_row($rows_thrName))
{
$data[]=$cname;
}
if (!$q) return;
foreach ($data as $value) {
if (strpos($value, $q) !== false) {
echo mb_convert_encoding($value,'utf-8','big5')."\n";
}
}
?>
請高手幫下忙,想左幾日的,都行吾到。 除了網頁、資料庫編碼要注意是否正確之外,另外就是 server 端街收到的資料會被編碼,
例如「許功蓋」會變成 "%u8A31%u529F%u84CB" 的字串,
所以要再用以下的方式取為原字串:
[code php]
$q = join('', json_decode('["'. strtr($_GET['q'], '%u', '\u') . '"]'));
[/code] 我的網頁、資料庫編碼是用"big5"的。
我加的$q = join('', json_decode('["'. strtr($_GET['q'], '%u', '\u') . '"]'));中文仍然沒有反映。 哦,我剛測試的結果,如果你的網頁是用 big5,則 server 端接收到的仍是 UTF-8 的字串喔,如果要能正確處理 big5,則要改回 big5 編碼:[code]$q = iconv('UTF-8', 'big5', $_GET['q']);[/code] 多謝wmh的幫忙,可以的。
頁:
[1]