Skip to content Skip to sidebar Skip to footer

Stuck At Jquery Ajax Db Encoding For Iso-8859-9 Charset

I am trying to pass data to db via my Jquery application. I have serious problems with encoding. Current page encoding is iso-8859-9. I've made my ajax page encoding to iso-8859-9

Solution 1:

The application/x-www-form-urlencoded does not have a charset. It's simply ASCII characters. Specifying charset there will do nothing.

jQuery will normally urlencode your data as is specified:

  1. Encode to UTF-8
  2. Percent-encode

So:

$.post( "test.php", {data:'İZİN'}); //Shorthand for$.ajax

Actually posts this to server:

data=%C4%B0Z%C4%B0N

When you access $_POST['data'] with php, they have been turned into bytes (0xC4B05AC4B04E), so echoing them will give you malformed data:

header("Content-Type: text/html; charset=ISO-8859-9");
echo$_POST['data'];
// Ä°ZÄ°N

You can test this is true with:

header("Content-Type: text/html; charset=ISO-8859-9");
echo"\xC4\xB0\x5A\xC4\xB0\x4E";
// Ä°ZÄ°N

In PHP you need convert it to ISO-8859-9 as soon as you receive it:

<?php
header("Content-Type: text/html; charset=ISO-8859-9");
$data =  "\xC4\xB0\x5A\xC4\xB0\x4E"; //$_POST['data'];$proper = mb_convert_encoding( $data, "ISO-8859-9", "UTF-8" );
echo$proper;
//İZİN

Note that it's just much easier to use UTF-8 everywhere because it's pretty much the best encoding and the web loves it. If you use any other encoding then you will have to be on your toes all the time.

Post a Comment for "Stuck At Jquery Ajax Db Encoding For Iso-8859-9 Charset"