Click to See Complete Forum and Search --> : Struggling with UTF-8 AJAX
TygerTyger
01-19-2008, 12:19 PM
PHP and MySQL both are working fine with UTF-8 and when POSTing data without using AJAX then data is saved and retrieved correctly. However, when saving non-standard characters using AJAX, they look like this: %u56FD.
I am using 'application/x-www-form-urlencoded; charset=utf-8' and encodeURIComponent(text) in the javascript and sending utf-8 charset headers in PHP.
How should I go about decoding the javascript POSTed string so it is returned the same as a PHP POSTed string?
Many thanks, cos this is driving me nuts.
NogDog
01-19-2008, 01:52 PM
urldecode (http://www.php.net/urldecode)()
TygerTyger
01-19-2008, 02:11 PM
It's already urldecoded, however the urldecoded text is exactly the same as the non-urldecoded text (nothing silly going on like editing the wrong text). Does that help?
NogDog
01-19-2008, 03:00 PM
I wonder if it's getting encoded twice: once by the explicit call to encodeURIComponent(text) then again by the form transmission mechanism? Maybe try taking out that explicit encoding call and see what happens?
TygerTyger
01-21-2008, 07:23 AM
Nope, that's not it! Both the encodings are required for it to work.
I'm a little further along, the problem is described here (http://webreflection.blogspot.com/2006/11/php-and-javascript-encoding-comparison.html) and a function posted here (http://php.oregonstate.edu/manual/en/function.iconv.php#53365) appears to undo the javascript encoding mess that it creates.
I say appears, because what I have now is a ? instead of a ç and when writing to the database, everything from the ? character onwards disappears. Whilst the javascript encoding seemed to have an obvious explanation, this doesn't.
Again, everything utf-8 works just fine when not using javascript.
Anyone?