Jump to content

Javascript + AJAX + PHP


DarkReaper

Recommended Posts

Hello i am having a problem with ajax and php encodings ... i have a string encoded in cp1251 in a php variable, i save it to a file (using ajax and php), then with ajax i read it from the file and display it ... but only screwed up characters appears ...

The strangest thing is that if i type a message (input box, again cp1251 encoding) and then handle it to be saved to a file and display it, everything is okay ...

Do you have any ideas why is this happening?
Link to comment
Share on other sites

Hi,
That's one of the drawbacks of Ajax. Whenever you send any data with xmlhttprequest object, it's automatically converted into UTF-8 and when received it's UTF-8 encoded. I had a similar problem. I feel there is no solution to it than again converting it to your desired encoding via PHP. On MSDN I had read that Microsoft is thinking of resolving this issue in future release of IE.
Let's see when it happens

Regards
Rahul
Link to comment
Share on other sites

The best way is to pass your total encoding to utf-8 which supports most of the characters.

But instead of working with encodings try replacing problematic characters with html entities. This had solved my character encoding problem from xmlhttprequest thing. In longer textes it slows but 1 or 2 seconds is acceptable if everything is seen ok.
Link to comment
Share on other sites

I think your XMLHTTPRequest object send UTF-8 to your php script. In your php script then, you can convert it to your original encoding like this:

[code=php:0]iconv("UTF-8","YOUR_ENCODING",$_GET['var'])[/code]

and do whatever you want to do with it. When you need to return info from your php script, use XML if you need to return non-ASCII chars, like this:

[code]
header('Content-Type: application/xml');
$out = '<?xml version="1.0" encoding="ORIGINAL_ENCODING?>' . "\r\n";
$out .= '<report xml:lang="LANG_HERE"><message>'.$message.'</message></report>';
echo $out;
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.