Php+ajax the encoding problem that is encountered when validating a user name

Source: Internet
Author: User
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!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.