e gazi e gazi - 3 months ago 22
jQuery Question

UTF-8 encoding issue with turkish characters in php, ajax, json, jquery ,mysql

I am having trouble with the character encoding. I have tried lots of things, but got no result.
Here is the case: I have some stored procedures in my DB and I send notifications to the users. Here it is:

some calculations..
IF @howmanyknew=0 || @howmanyknew>=6 THEN
SET @memberID=(SELECT userID FROM myDB.users WHERE ticketID=@ID);
INSERT INTO notifications (senderID,receiverID,messageContent,isRead)VALUES(1,@memberID,"Some turkish characters like ö ç ş ğ ü ı İ",0);
END IF;


And here is the structure of my notifications table:

id (INT) (PRIMARY_KEY)
senderID (INT)
receiverID (INT)
messageContent (TEXT) utf-8_turkish_ci
isRead (INT)


After adding this, a notification ball appears to the user just like in the facebook. When the user clicks it, he/she sees the message:


  1. No turkish character, no problem:

    Case1

  2. Some turkish characters: ö,ç,ü. I got this message in the console:


    (index):167 Uncaught TypeError: Cannot read property 'counter' of null


    Here, ajax can't get the variables from the back php page (fetchMessages.php) via
    json_encode()

  3. The rest turkish characters: ş,ı,ğ.

    Case 3



Here is the relevant parts of my
index.php
and
fetchMessage.php


IMPORTANT: Both are coded with "UTF-8 without BOM"

index.php:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<script>
.
.
$.ajax({
type:"POST",
url:"fetchMessages.php",
data: '{"id":"'+id+'"}',
dataType: "json",
success: function(returnedData) {
var n=returnedData.counter;
$(".content").empty();
for(i=0;i<n;i++)
{
$(".content").append("<span style='display:block;text-align:left;font-size:9px;font-style:italic;background:gray'>"+returnedData.nick[i]+":</span>");
$(".content").append("<div class='divtext' contentEditable>"+returnedData.msg[i]+"</div>");
}
$(".content").fadeIn("slow");
},
error: function(returnedData){
alert(returnedData);
}
});
.
.
</script>


fetchMessages.php:

<?php
//header('Content-Type: application/json; charset=iso-8859-1');
//setlocale(LC_ALL, 'tr_TR');
include("conn.php");
//iconv('cp1252', 'utf-8', "\x80 and \x95")-> "\xe2\x82\xac and \xe2\x80\xa2";
$input = file_get_contents('php://input');
$result=json_decode($input);
$id=$result->id;
$result=dbquery("select * from notifications where receiverID=".$id." and isRead=0");
$senders=array();
$msgs=array();
$ids=array();
$counter=0;
while ($row = $result->fetch_assoc())
{
$m=$row["messageContent"];
//$m=iconv("UTF-8", "ISO-8859-1//TRANSLIT", $m), PHP_EOL;
$msgs[$counter]=$m;
$ids[$counter]=$row["id"];
$senderID=$row["senderID"];
$result=dbquery('select * from usertbl where userID='.$senderID.'');
$s=$result->fetch_assoc();
$senders[$counter]=$s["username"];
dbupdate("notifications", array("isRead"=>1), array("id"=>$ids[$counter]));
$counter=$counter+1;
}
echo json_encode(array(msg=>$msgs,nick=>$senders,counter=>$counter,ids=>$ids));
?>


You also can see the some comment outs in the php page. I have tried them too. But as I said, no result.

AND If I convert them from "utf-8 without BOM" to "utf-8" I only get an alert dialog box for the characters in Case 2 above:

[object XMLHttpRequest]


However, I still get the question mark for the rest.

Answer

You can try adding

"SET NAMES UTF-8" 

Sql statement just after your connection to database.