我的 AJAX 中的编码有问题。
我有一个丹麦基金会的网站——这些基金会的名称包括特殊字符“Æ”、“Ø”和“Å”。当我在下拉菜单中进行过滤时,特殊字符会显示为“?”。
JavaScript:
function findLegater(val1, val2, val3) {
var stu = document.getElementById(val1).value
var ud = document.getElementById(val2).value
var kva = document.getElementById(val3).value
if (stu=="N" && ud =="0" && kva =="0") {
document.getElementById("txtHint").innerHTML="";
}
else {
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getlegat.php?s="+stu+"&u="+ud+"&k="+kva,true);
xmlhttp.send();
}
}
PHP接收数据:
$s=$_GET["s"];
$u=$_GET["u"];
$k=$_GET["k"];
include 'msql/msql-connect.php';
if ($s!= "N" && $u != "0" && $k!="0") {
$query = "SELECT * FROM legater WHERE studiegrad LIKE '%$s%' and studie LIKE '%$u%' and kvartal LIKE '%$k%'";
}
else if ($s!= "N" && $u == "0" && $k=="0") {
$query = "SELECT * FROM legater WHERE studiegrad LIKE '%$s%'";
}
else if ($s!= "N" && $u != "0" && $k=="0") {
$query = "SELECT * FROM legater WHERE studiegrad LIKE '%$s%' and studie LIKE '%$u%'";
}
else if($s== "N" && $u != "0" && $k=="0") {
$query = "SELECT * FROM legater WHERE studie LIKE '%$u%'";
}
else if($s== "N" && $u != "0" && $k!="0") {
$query = "SELECT * FROM legater WHERE studie LIKE '%$u%' and kvartal LIKE '%$k%'";
}
else if($s== "N" && $u == "0" && $k!="0") {
$query = "SELECT * FROM legater WHERE kvartal LIKE '%$k%'";
}
else if($s!= "N" && $u == "0" && $k!="0") {
$query = "SELECT * FROM legater WHERE studiegrad LIKE '%$s%' and kvartal LIKE '%$k%'";
}
// Execute query
$result = mysql_query($query) or die ("Error in" . $query);
$number = mysql_num_rows($result);
// Check result
if ($number == 0) {
echo "Ingen legater fundet";
}
else {
while ($row = mysql_fetch_array($result)) {
echo "<li><a href=\"legat.php?id=" . $row['id'] . "\">" . $row['navn'] . "</a> </li>";
}
}
// free result set memory
mysql_free_result($result);
// close connection
mysql_close($connection);
最后,这是我的 HTML 标头:
<meta charset="UTF-8">
我不知道如何修复字符。我做了以下事情:
- 检查我的本地主机(我目前用于测试)是否使用 UTF-8
- 确保我的 HTML 具有 UTF-8 元字符集
- 我所有的文件都是在 Notepad++ 中创建的 - 我已经确保所有文件都正确创建为 UTF-8(无 BOM)
非常感谢所有帮助!