Why is garbled characters displayed when jqueryajax submits Chinese? The submit.htm code on the submission page is as follows: HTMLcode & lt ;! DOCTYPEhtmlPUBLIC & quot;-W3CDTDXHTML1.0TransitionalEN & quot; www. w3.orgTRxhtml1DTDxhtml1-transit jquery ajax submit Chinese garbled characters?
The submit.htm code is as follows:
HTML code
Welcome
... $. Ajax ({type: "POST", url: "xx/add_post.php", data: 'AA = '+ escape ($ (' # AA '). val () + '& bb =' + $ ('# bb '). val (), success: function (html) {alert ('added successfully! ');}});....
The received data page is add_post.php.
PHP code
getMessage(); } $stmt=$pdo->prepare("insert into acom_a(aa, bb) values(:aa, :bb)"); $stmt->execute(array(":aa"=>$_POST["aa"], ":bb"=>$_POST["bb"]));?>
The aa field is garbled in Chinese, and bb is normal in English!
------ Solution --------------------
Execute add_post.php file separately to insert Chinese garbled characters?
------ Solution --------------------
EncodeURI ($ ('# bb'). val (); in this way, encodeURI is used for variables that may contain Chinese characters.
------ Solution --------------------
Discussion
Reference:
You don't seem to have the tools or capabilities to debug ajax.
$ Pdo = new PDO ("mysql: host = localhost; dbname = acom", "root", "abcdefghi", array (PDO :: MYSQL_ATTR_INIT_COMMAND => "set names utf8 "));
Your PDO settings have solved the problem. thank you!
Also ask ......
------ Solution --------------------
Discussion
Is there a way to stop executing this statement after it is modified in the configuration file?
------ Solution --------------------
The problem is solved!
Array (PDO: MYSQL_ATTR_INIT_COMMAND => "set names utf8 ")
Indicates that after the mysql result is connected, the set names utf8 command is executed immediately to set the character set for communication to UTF-8.
Because mysql databases are independent and public. Therefore, it is unlikely that you will only be installed based on the parameters you need.
Therefore, it is necessary to declare the character set during use.
Mysql is personalized for PDO. For declared character sets, he needs to execute SQL commands. if oralce is declared in dsn