Solving European characters (Western charset) problem with Ajax and PHP

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.

14 thoughts on “Solving European characters (Western charset) problem with Ajax and PHP

  1. 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?

  2. @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”);

  3. Felix

    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.

  4. if you wont to use UTF-8 you need to save file (php/html) with UTF-8 encoding too

  5. Great it helps a thanks lot man… nice work..

  6. 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!!!!

  7. Merlin

    great article !

  8. Faisal

    thanks man …. thankx ….. u hlep me alot

  9. 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 .

  10. Ctibor

    Thaaaaaaaank youuuuu.. my problem is solved!!! Yeah.. finally :)

  11. I have used Windows-1256 and Windows-1252.Both are not working.
    The character is ú and it is displaying as úl….The charset is coming from AJAX (Classic ASP)

    I have tried with ISO and UTF-8 as well.None of the Charset working ….
    Any one is there to help me ?

  12. we are working on real estate project for Europe based client. your blog help me to solve the characters issue. cheers.

  13. Attila

    Thank you, i was wery disappointed, i thought i have to use UFT-8 instead of latin2 to make it work properly, i was searching a lot but didn’t found my fix for it, so i decided to make it with utf-8, used it wasn’t working also OMG! Finally i found this post, it helped me out, i had no idea that i have to put this header in the process php file.
    Thank you!

    Attila.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>