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

Advertisement

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% [?]

Enter your email address and get free tutorials, tips and tricks of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

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?

10 Comments on “Solving European characters (Western charset) problem with Ajax and PHP”

  • Endijs Lisovskis wrote on 15 September, 2008, 20:51

    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?

  • Roshan wrote on 16 September, 2008, 5:40

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

  • Felix wrote on 16 September, 2008, 10:30

    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.

  • vlado wrote on 16 September, 2008, 11:55

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

  • oscar wrote on 17 November, 2008, 6:41

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

  • Harshal wrote on 22 June, 2009, 4:47

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

  • Merlin wrote on 13 July, 2009, 6:44

    great article !

  • Faisal wrote on 21 August, 2009, 21:09

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

  • jack wrote on 21 November, 2009, 14:25

    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 .

Trackbacks

  1. pligg.com

Write a Comment