开发者

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

开发者 https://www.devze.com 2023-02-07 02:39 出处:网络
I have a form that submits data using the jQuery form plugin.From there it goes through some server side ha开发者_运维百科ndling and finally stored in a MySQL db.

I have a form that submits data using the jQuery form plugin. From there it goes through some server side ha开发者_运维百科ndling 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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号