Jump to content


Photo

Javascript + AJAX + PHP


  • Please log in to reply
4 replies to this topic

#1 DarkReaper

DarkReaper
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 03 September 2006 - 07:34 AM

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?

#2 expertsystems

expertsystems
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 05 September 2006 - 07:10 AM

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

#3 DarkReaper

DarkReaper
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 11 September 2006 - 09:47 PM

How can i convert it again to my desired encoding?

Example i send cp1251 encoding to ajax then when it converts it to utf8, i should convert it again to cp1251??? Am i right?

#4 radalin

radalin
  • Members
  • PipPipPip
  • Advanced Member
  • 179 posts

Posted 12 September 2006 - 07:25 AM

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.
Roy Simkes
Yet Another Parkyeri Developer

#5 yaba

yaba
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 13 September 2006 - 04:32 AM

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:

iconv("UTF-8","YOUR_ENCODING",$_GET['var'])

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:

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;





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users