0

So im trying to send data from an HTML page through Ajax, to a PHP page.

Thats the piece of jQuery code that im using:

$.ajax({
    url: "test.php",
    type: "POST",
    data: {
        name: "João"
    }
}).done(function (data) {
    alert(data);
})

As you can see, the parameter im sending is "João". Before making the Ajax request jQuery encodes it on the background, "João" becomes "Jo%C3%A3o" which is double encoded UTF-8.

My problem arises when the request is sent and PHP tries to decode it on the background. PHP decodes automatically it only once when I use $_POST, so instead of getting "João" I get "João". That happens because PHP is decoding every % individually, so %C3 becomes à and %A3 becomes £.

If I try to decode it manually through utf8_decode() it will work, but im here to know if there's a better solution. What I really need is a way for PHP to decode my data correctly, even if it's double-encoded, or even triple-encoded.

user937450
  • 713
  • 5
  • 11
  • 20
  • 2
    That is not "double encode"... it is how UTF-8 has to be encoded. If you wish to have a single byte representing `ã`, then you need to specify a supported character set before sending the data. – Lekensteyn Feb 16 '13 at 22:38
  • Remember you don't have to repost questions as you can edit and improve the [old one](http://stackoverflow.com/q/14915660/1331430). But as the other wasn't even close to a question and you've already posted a new one, you can at least delete the other. – Fabrício Matté Feb 16 '13 at 22:39
  • On topic, I've never had that issue. Did you set the charset of the page to UTF-8? – Fabrício Matté Feb 16 '13 at 22:40
  • Lekensteyn I understood. My problem is that you can't change the charset before sending the Ajax data when using POST type. It will always be sent as UTF-8. So in this case I need a way to fully decode my UTF-8 data on PHP. – user937450 Feb 16 '13 at 23:00

1 Answers1

2

That's not double-encoded, it's correct UTF-8. It looks like the PHP is expecting latin-1 encoding instead, and is showing you what the same bytes would mean if they were not UTF-8.

In this case, since your characters seem to be below 0xFF, you could also URL-encode them first as Jo%E3o in latin-1 if you can't work out how to have PHP recognize UTF-8.

Jeremy Griffith
  • 306
  • 2
  • 7
  • I did a test on my text editor. I wrote "João" using UTF-8 encoding, then I converted it to ANSI and the result was the exact same thing "João". So it looks like that PHP is trying to decode my UTF-8 data to ANSI instead of UTF-8 itself. – user937450 Feb 16 '13 at 23:38
  • So here is what is happening: 1 - The data is sent to PHP with UTF-8 encoding 2 - PHP receives it and try to decode it with a wrong encoding (ANSI). The data HAS to be sent as UTF-8, so I need to correct the PHP part. – user937450 Feb 16 '13 at 23:42
  • http://stackoverflow.com/questions/9351694/setting-php-default-encoding-to-utf-8 you could also try the php function utf8_decode. – James Feb 16 '13 at 23:44