I’ve got a little chatbox, everything’s working, except for sending special latin chars like äöü.
This is the post which sends the input, as soon enter is pressed (it’s placed in the body, not in the header, couldn’t figure out how to put it in the header –> vBulletin Template):
<script type="text/javascript">
$("#shout").keypress(function(e) {
if(e.keyCode == 13) {
$.ajax({
url: "http://example.com/api.php",
contentType: "text/html; charset=ISO-8859-1",
data: {
action: "shout",
// This is what I found on the internet to
// encode the data, but didn't help.
message: $('<div />').text($("#shout").val()).html()
},
success: function() {
$("#shout").val("");
}
})
}
});
</script>
On the server side
// Insert shout
case "shout":
if(isset($_GET['message']) && $_GET['message'] != "") {
$mc->insertShout($userid, mktime(), $_GET['message']);
}
break;
insertShout logic
public function insertShout($userid, $date, $message, $notification = 0) {
$sql = "INSERT INTO
vb_chatbox_shout (userid, dateline, message, message_raw, notification)
VALUES
(?, ?, ?, ?, ?)";
$stmt = $this->db->prepare($sql);
$stmt->bindParam(1, $userid);
$stmt->bindParam(2, $date);
$stmt->bindParam(3, $message);
$stmt->bindParam(4, $message);
$stmt->bindParam(5, $notification);
if(!$stmt->execute()) {
var_dump($stmt->errorInfo());
}
return 1;
}
I’ve tried using utf-8 as charset, but no luck. Also, post didn’t help. Using this kind of ajax to pull the chatbox content works like a charm (special chars directly inserted to db with phpmyadmin are showed correctly).
The problem is not in inserting the chars to the database. When I dump the $_GET var at the first entry point, it’s already messed up, so the problem’s between jQuery and PHP.
New headers
GET /api.php?action=shout&message=%25C3%25B6%25C3%25A4%25C3%25BC HTTP/1.1
Host: example.com
Connection: keep-alive
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5
Accept: */*
Referer: http://example.com/forum/forum.php
Accept-Encoding: gzip,deflate,sdch
Accept-Language: de
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: skimlinks_enabled=1; bb_lastvisit=1316066700; bb_lastactivity=0; bb_userid=1;
HTTP/1.1 200 OK
Date: Mon, 28 May 2012 17:26:14 GMT
Server: Apache
X-Powered-By: PHP/5.2.9
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8
replace
with
in javascript, and then replace
with
and add
at the beginning of PHP file.
if
urldecodeat php doesn’t work, try replacing it withrawurldecode().