PHP Ajax MySQL Test
I ran into this problem when I was testing the code.
The code is as follows:
index.php
;
Ajax.js
var xmlhttp;function s_xmlhttprequest () {if (window. ActiveXObject) {xmlHttp = new ActiveXObject (' Microsoft.XMLHTTP ');} else if (window. XMLHttpRequest) {xmlHttp = new XMLHttpRequest ();}} function funphp100 (name) { var f=document.myform.user.value; S_xmlhttprequest (); Xmlhttp.open ("GET", "for.php?id=" +f,true); xmlhttp.onreadystatechange = Byphp;xmlhttp.send (null );} function byphp () { if (xmlhttp.readystate = = 1) {document.getElementById (' php100 '). InnerHTML = "";} if (xmlhttp.readystate = = 4) {if (Xmlhttp.status = =) { var byphp100 = Xmlhttp.responsetext; document.getElementById (' php100 '). InnerHTML = byphp100;}}}
for.php
I tested both in the local environment and in the BAE Environment, respectively.
Has appeared the Chinese character can not be normal transmission phenomenon, the database already has "thousand hands" this user, but still prompt can use this user, seemingly is the problem of coding.
, ask the Expert answer ~
Reply to discussion (solution)
The problem occurs:
Don't sink, ask the expert to answer ~
PHP files are unified with the browser code.
mysql_query ("Set names UTF8"); Add this sentence before the enquiry
Sexy year, change the code bar, uncensored better.
Remember to give points (⊙o⊙) OH
Lz, the Ajax you use is not submitted with a form form, in other words, your value is passed through the address bar.
I remember using the address bar to send Chinese language will appear garbled.
You can be in
Xmlhttp.open ("GET", "for.php?id=" +f,true);
Add a sentence:
encodeURI (f);
F=encodeuri (f); Just forget to assign value! Give it a try!
When doing AJAX applications, it is best to use Utf-8 as the default character set for the site
Because XMLHttpRequest components always send data in Utf-8 encoding
Of course, the use of GBK is also possible, after all, compared to utf-8 for Chinese characters to save 1/3 of storage and transmission capacity
But you need to look at the points.
1, the JS end of the data sent to use encodeURIComponent encoded into Utf-8 URL encoding
2, PHP side to use ICONV conversion received data for GBK
3, the PHP side should return the data before the header (' CONTENT-TYPE:TEXT/HTML;CHARSET=GBK '); Used to declare that the returned data is GBK
Header ("Charset=utf-8"); add a look in the for.php page,
The database should also be mysql_query ("Set names UTF8");
It is possible that the database encoding problem is:
Show variables like ' char% '
If you're not mistaken, it's GBK.
F=encodeuri (f); Yes, the 5 floor is right!