AlexV
03-03-2009, 08: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:
<?php
header('Content-Type: text/html; charset=UTF-8');
if (array_key_exists('str', $_GET))
$myStr = $_GET['str'];
else
$myStr = '';
echo($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');
request.send(null);
}
}
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));
ajaxResults.appendChild(newP);
}
}
else
alert('HTTP status ' + request.status);
}
}
TestUTF();
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.
Thanks!
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:
<?php
header('Content-Type: text/html; charset=UTF-8');
if (array_key_exists('str', $_GET))
$myStr = $_GET['str'];
else
$myStr = '';
echo($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');
request.send(null);
}
}
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));
ajaxResults.appendChild(newP);
}
}
else
alert('HTTP status ' + request.status);
}
}
TestUTF();
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.
Thanks!