RDL
RDL

Reputation: 7961

How can I get non-english characters from ajax form to MySQL

I have a form that submits data using the jQuery form plugin. From there it goes through some server side handling and finally stored in a MySQL db.

Upon submitting these characters áâãäï they become sent to the server as áâãäï' (before hitting the database). The same type of thing happens with Japanese characters, etc.

I have tried both UTF-8 and ISO-8859-1 for the form encoding, page encoding, php server side encoding and db encoding. Still no luck.

Upvotes: 2

Views: 1060

Answers (2)

PleaseStand
PleaseStand

Reputation: 32102

Internally, JavaScript works with UTF-16 strings, and functions such as encodeURIComponent() encode characters to UTF-8, so it is probably not the JavaScript code that is the problem. The problem is likely within your PHP code.

  • Every page sent by the server (including the page the form is on) has to have the header Content-type: text/html;charset=UTF-8. You can check this using Firebug's Net panel.

  • Always set the character encoding of your MySQL connection before making any queries. Otherwise, MySQL will interpret each byte of the UTF-8 string as a Latin-1 character, which can easily cause what you describe. Use code such as:

    mysql_query("SET NAMES 'utf8'");    // works in old versions of PHP (< 5.2)
    mysql_set_charset('utf8',$conn);    // works in PHP 5.2+
    
  • Keep in mind that some (not all) PHP string functions will mangle Unicode text.

Upvotes: 2

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114417

The characters are probably being pasted in from WORD (ISO-8859-1). Ajax expects UTF-8 ONLY, so you'll have to convert them on the client before sending them to the server.

See: Change encoding from UTF-8 to ISO-8859-2 in Javascript

Upvotes: 1

Related Questions