views:

72

answers:

1

Posting non-latin based languages with ajax + jquery doesn't save to mysql the correct text.

What I have done is this:

  • I am getting multiple translated words from Google's translation api.
  • The ajax request is showing the correct translations for all languages.
  • But when i try and insert this into the db it shows up in php my admin as garbled text
  • I added AddDefaultCharset UTF-8 to .htaccess file on the root.
  • I tried setting the header in php to utf-8 and this did not work.
  • I have tried adding a contentType to ajax setup but this didn't work also.

Any suggestions appreciated.

I am using the following jquery code :

I am able to see the translated text sent to the save_translation.php page

 var d = {"english":"<?php echo $w;?>","addwords":translated};
   data = jQuery.param(d);
   $.ajax({
     type:"POST",
     data:data,
     url:"save_translation.php"
     });

Each field is set to utf8_general_ci

UPDATED : responses:

Ajax post

english:thank you addwords:baie dankie|falemnderit|شكرا|дзякуй|благодаря|gràcies|谢谢|谢谢|謝謝|hvala vam|děkuji|tak|dank u|thank you|tänan teid|salamat|kiitos|merci|grazas|Danke|σας ευχαριστώ|תודה|धन्यवाद|köszönöm|þakka þér|terima kasih|go raibh maith agat|grazie|ありがとう|감사합니다|paldies|ačiū|Ви благодарам|terima kasih|nirringrazzjak|takk skal du ha|تشکر از شما|dziękuję|obrigado|mulţumesc|спасибо|хвала|ďakujem|hvala|gracias|asante|tack|salamat|คุณขอบคุณ|teşekkür ederim|спасибі|cảm ơn bạn|ddiolch 'ch|אַ דאַנק

Server Side ouput

english : thank you

baie dankie|falemnderit|شكرا|дзякуй|благодаря|gràcies|谢谢|谢谢|謝謝|hvala vam|děkuji|tak|dank u|thank you|tänan teid|salamat|kiitos|merci|grazas|Danke|σας ευχαριστώ|תודה|धन्यवाद|köszönöm|þakka þér|terima kasih|go raibh maith agat|grazie|ありがとう|감사합니다|paldies|ačiū|Ви благодарам|terima kasih|nirringrazzjak|takk skal du ha|تشکر از شما|dziękuję|obrigado|mulţumesc|спасибо|хвала|ďakujem|hvala|gracias|asante|tack|salamat|คุณขอบคุณ|teşekkür ederim|спасибі|cảm ơn bạn|ddiolch 'ch|אַ דאַנק|

Each field is set to utf8_general_ci

PhpMyAdmin

thank you|baie dankie|falemnderit|شكرا|дзÑкуй|благодарÑ|grà cies|谢谢|谢谢|è¬è¬|hvala vam|dÄ›kuji|tak|dank u|thank you|tänan teid|salamat|kiitos|merci|grazas|Danke|σας ευχαÏιστώ|תודה|धनà¥à¤¯à¤µà¤¾à¤¦|köszönöm|þakka þér|terima kasih|go raibh maith agat|grazie|ã‚ã‚ŠãŒã¨ã†|ê°ì‚¬í•©ë‹ˆë‹¤|paldies|aÄiÅ«|Ви благодарам|terima kasih|nirringrazzjak|takk skal du ha|تشکر از شما|dziÄ™kujÄ™|obrigado|mulÅ£umesc|ÑпаÑибо|хвала|Äakujem|hvala|gracias|asante|tack|salamat|คุณขภบคุณ|teÅŸekkür ederim|ÑпаÑибі|cảm Æ¡n bạn|ddiolch 'ch|×Ö· ד×Ö·× ×§

+1  A: 

Hello!

Probably it will be enough to use JavaScript function encodeURIComponent.

If you don't add parameters manual to the URL like myUrl + '?param1=' + param1 + '&param2' + param2, but use construction myUrl + '?' + jQuery.param({param1:param1, param2:param2}) then encoding with respect of the function function encodeURIComponent will make jQuery for you. In the case all '&' characters and 'paramX=' strings will be also added for you.

But the best way to use data parameter of the jQuery.ajax method. If you use

jQuery.ajax({
    url: myUrl,
    data: {param1:param1, param2:param2},
    //...
});

then your URL will be appended with '?param1=' + param1 + '&param2' + param2 with the corresponding encoding full from jQuery.

If my tips not helps you, please post your code example.

UPDATED: After you posted test data it was clear, that you have no problem with ajax request. You show that the data of the server look like absolutely correct. So your problem is somewhere between PHP and MYSQL. I works with no from this two products and can now not really help you. Probably information from the following link could help you http://stackoverflow.com/questions/1370915/string-issue-of-utf-8-encoding-with-php-and-mysql. You can try also

mysql_query("SET NAMES 'utf8'"); 
mysql_query("SET CHARACTER SET 'utf8'");

I wish you much success and quick solving of your problem. Regards

P.S. In your ajax request you can directly use data: {"english":"<?php echo $w;?>","addwords":translated}. The call jQuery.param will make jQquery for you because the type of data is not a string. But it's absolutely independ on your problems.

Oleg
ok oleg i added that information to the original question
jason
Thanks a million! Using the two SQL statements got it working! Which i should have tried a few days ago because i already had to use set charset utf8 before selecting a language - jason
jason