Solving European characters (Western charset) problem with Ajax and PHP
- Monday, September 15, 2008, 18:32
- ajax, how-to, php, tips and technique
- 10 comments
Today, I would like to tell you how to handle character set problem which occurs during the data fetched from PHP using Ajax mainly on the western characters(European charset).Lots of people asked me about this problem where these European charset is displayed in unreadable format after fetching it from ajax.
Problem with European Charset with Ajax and PHP
Suppose that I’ve the following code in PHP file, which output the string which contain the european characters.
<?php $str="€-accentuée"; echo $str; ?>
Now, let’s use the Ajax from JavaScript using Jquery Code to fetch the data from PHP.
<script type="text/javascript">
$(function()
{
$('#charsetdiv').load("test2.php");
});
</script>
<div id="charsetdiv"></div>
If you’ve not used jQuery then you might be wondering about the ajax code, you can check the benefits of jquery from this post and download free ebook of jQuery.
After getting value from Ajax, the division with id “charsetdiv”, let’s look at the output.
?-accentu?e
Solving western Character Set problem with Ajax and PHP
The above problem is occurred because of not defining proper charset to these European characters. We can solve this problem by sending the header which define the appropriate Character Set for these characters.Let’s see, how I solve this problem just adding the header which define the charset Windows-1256 – which support Arabic as well as European characters.
<?php
header("Content-Type: text/html; charset=Windows-1256\n");
$str="€-accentuée";
echo $str;
?>
After adding that header in PHP, you’ll get the appropriate european characters in your Ajax enabled application.
Popularity: 5% [?]
Related Posts
» Solving Floating point number precision lost problem in PHP
» How to solve the problem of retrieving same value by Ajax – Browser Cache Problem
» About Me
» How to control the case of characters using CSS?

Why not to use UTF8 headers? If you use exact windows-yyyy, then for each locale (Baltic, etc. etc) you would need to specify exact yyyy and then to deal with encoding problems in Web site (if you are displaying different languages).
So – where is the point not to use UTF8?
@Endijs – Well, You’re absolutely right and eventhough I’ve thought about using UTF-8 encoding and it didn’t work at all…try it ..
header(“Content-Type: text/html; charset=UTF-8\n”);
but the using any of the following header did work well…
header(“Content-Type: text/html; charset=Windows-1256\n”);
or
ISO charset for European characters,
header(“Content-Type: text/html; charset=ISO-8859-1\n”);
Roshan, yesterday I was a same problem rendering a page in Firefox or IE. I decide put windows-1256 and voila!…work!.
Somebody can explain it?, please.
if you wont to use UTF-8 you need to save file (php/html) with UTF-8 encoding too
Great it helps a thanks lot man… nice work..
I had to code as u have shown in a previous example of triple drop down but i am facing the problem of not getting Content in readable format…As i am working in Norwegian language how can i solve this problem…Please help!!!!
great article !
thanks man …. thankx ….. u hlep me alot
header(“Content-Type: text/html; charset=UTF-8″);
header(“Content-Type: application/x-javascript; charset=UTF-8″);
i used all this script can’t in textbox request ajax .