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.
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|×Ö· ד×Ö·× ×§
Probably it will be enough to use JavaScript function
encodeURIComponent.If you don’t add parameters manual to the URL like
myUrl + '?param1=' + param1 + '¶m2' + param2, but use constructionmyUrl + '?' + jQuery.param({param1:param1, param2:param2})then encoding with respect of the function functionencodeURIComponentwill make jQuery for you. In the case all ‘&’ characters and ‘paramX=’ strings will be also added for you.But the best way to use
dataparameter of thejQuery.ajaxmethod. If you usethen your URL will be appended with
'?param1=' + param1 + '¶m2' + param2with the corresponding encoding full fromjQuery.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
ajaxrequest. 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 string issue of utf-8 encoding with PHP and MySQL?. You can try alsoI wish you much success and quick solving of your problem.
Regards
P.S. In your
ajaxrequest you can directly usedata: {"english":"<?php echo $w;?>","addwords":translated}. The call jQuery.param will make jQquery for you because the type ofdatais not a string. But it’s absolutely independ on your problems.