View Full Version : AJAX with PHP encoding issue

03-03-2009, 09:38 PM
My whole website is UTF-8 encoded, all my files (.JS, .HTML, .PHP) are saved in UTF-8 (no BOM).

I always use

header('Content-Type: text/html; charset=UTF-8');
at the beggining of my PHP files.

I also use

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
for my HTML files.

But I still have an encoding issue...

I have an AJAX request that send me data to output in the DOM...

This is my PHP file that answers to the AJAX requests:

header('Content-Type: text/html; charset=UTF-8');

if (array_key_exists('str', $_GET))
$myStr = $_GET['str'];
$myStr = '';


This is the JavaScript that do the AJAX request:

function TestUTF()
var request = CreateRequest();
var unicodeTest = document.getElementById('unicodeTest');
if (request != null && unicodeTest)
unicodeTest = unicodeTest.value;
var ajaxUrl = 'test-ajax.php?str=' + escape(unicodeTest);

request.open('POST', ajaxUrl, true);
request.onreadystatechange = function(){TestRS(request)};
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

function TestRS(request)
if (request.readyState == 4)
if (request.status == 200)
var ajaxResults = document.getElementById('ajaxResults');
if (ajaxResults)
var newP = document.createElement('p');
newP.appendChild(document.createTextNode('AJAX write : ' + request.responseText));
alert('HTTP status ' + request.status);


As you can see this is a really simple project that get UTF-8 data for an HTML <input> element, sends it's value (content) through JavaScript then it's processed by PHP and then written back to the HTML (via the DOM) with JavaScript.

As far as I can see it's the PHP that screw my UTF-8 string... How can it be? I'm telling the browser that it's UTF-8 with header.

If I add utf8_encode to the echo all is right, but the purpose of this test is to get rid of these utf8_encode.

Anyone can help? I can provide full sources of this sample project if you want to try it.


03-03-2009, 10:19 PM
Found my solution!

Use encodeURIComponent instead of escape in JavaScript!

03-04-2009, 02:53 PM
Why does (JavaScript) escape do mess the string encoding when in UTF-8???

03-06-2009, 02:24 PM
I found out that escape only works with ASCII chars. When using Unicode (UTF-8) data with it, the output is corrupted. It even crash IE JavaScript andd XML engine.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum