0

I have a problem with the encoding in my AJAX.

I have a website with Danish foundations - the names of these include the special characters of 'Æ', 'Ø' and 'Å'. When I filter in my dropdown menus, the special characters are displayed with a '?'.

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 to receive the data:

$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);

Finally, this is my HTML header:

<meta charset="UTF-8">

I have no clue how to fix the characters. I have done the following:

  • Checked that my Localhost (which I currently use for testing) uses UTF-8
  • Made sure that my HTML has meta charset of UTF-8
  • All my files are created in Notepad++ - I have made sure, that all files were properly created as UTF-8 (no BOM)

All help is very much appreciated!

  • 1
    did you used `mysql_query("SET NAMES 'utf8'");` after selecting database? if not, its probably the reason –  Aug 02 '13 at 00:44
  • I had a very similar issue not too long ago with microsoft word curly quotes converting to question marks. I solved this by using `htmlentities($string, ENT_QUOTES, 'UTF-8')` when I was inserting the data into the database. From there I didn't have to do anything special to output it correctly. I know this is not an exact answer to your question, but hope it can help – Jacob Aug 02 '13 at 00:58
  • Akam, you solution worked! Thank you very much. I may now sleep again. – user2644120 Aug 02 '13 at 20:33

1 Answers1

0

try this to see what happens

 while ($row = mysql_fetch_array($result)) {
   $xxx .="<li><a href=\"legat.php?id=" . $row['id'] . "\">" . $row['navn'] . "</a>    </li>";
 }


echo utf8_encode($xxx);

As side Note: Mysql_* extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

A useful link Why shouldn't I use mysql_* functions in PHP

Community
  • 1
  • 1
Emilio Gort
  • 3,475
  • 3
  • 29
  • 44