Solve the problem of garbled Chinese characters during ajax interaction on pages encoded as gb2312

Source: Internet
Author: User
Tags php tutorial

Solve the problem of garbled Chinese characters during ajax interaction on pages encoded as gb2312
Ajax only supports the UTF-8 format and does not support the gb2312 encoding format. Therefore, garbled characters often occur when the gb2312 encoding program uses ajax. A solution was just found:

The data transmitted by the server is still gb2312 encoded. The client uses js to convert Chinese characters into UTF-8 encoded characters and displays them on the page.

Search. php tutorial
<? Php
Header ("content-type: text/html; charset = gb2312 ");
Include './search.htm ';
?>

Search.htm
<! Doctype html public "-// w3c // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "content-type" content = "text/html; charset = gb2312"/>
<Title> Advanced Search </title>
</Head>
<Body>
<H3> Advanced Search <Form method = "post" action = "">
School type:
<Select name = "schooltype">
<Option value = ""> All </option>
<Option value = "1"> Primary School </option>
<Option value = "2"> Junior High School </option>
</Select>
School name:
<Select name = "sid" id = "sid">
<Option value = ""> Select a School </option>
</Select>
</Form>
<Script type = "text/webpage effects">
Function ajax (settings ){
Var xhr = window. activexobject? New activexobject ("microsoft. xmlhttp"): new xmlhttprequest (), successed = false;
Xhr. open (settings. type, settings. url );
If (settings. type = 'post ')
Xhr. setrequestheader ('content-type', 'application/x-www-form-urlencoded ');
Xhr. send ((! Settings. cache? 'Time = '+ new date (). gettime () +' & ': '') + settings. data );
Settings. loader ();
Settimeout (function (){
If (! Successed ){
Alert ('resquest timeout! ');
Xhr. abort ();
        }
}, Settings. timeout );
Xhr. onreadystatechange = function (){
If (xhr. readystate = 4 & xhr. status = 200 ){
Settings. callback (xhr. responsetext. replace (/(^ s *) | (s * $)/g ,""));
        }
Successed = true;
    }
}
Function a (t ){
Ajax ({
Type: 'post ',
Url: 'Ajax. Php ',
Data: 'schooltype = '+ t,
Timeout: 8000,
Cache: true,
Loader: function (){},
Callback: function (d ){
Var arr = eval (d );
If (typeof (arr) = 'object '){
Var obj, option;
Document. getelementbyid ('Sid '). innerhtml = '';
For (var I = 0; obj = arr; I ++ ){
Option = document. createelement ('option ');
Option. value = obj [0];
Option. innerhtml = txt2utf8 (obj [1], '& #');
Document. getelementbyid ('Sid '). appendchild (option );
    }
   }
  }
})
}
Function txt2utf8 (string, prefix ){
For (var I = 0, utf8 = []; I <string. length; utf8.push (prefix | 'u') + string. charcodeat (I ++ )));
Return utf8.join ('');
}
A (0 );
</Script>
</Body>
</Html>

Ajax. php

<? Php
Header ("content-type: text/html; charset = gb2312 ");
$ Schooltype =! Empty ($ _ post ['schooltype'])? $ _ Post ['schooltype']: 0;
Switch ($ schooltype ){
Case 0:
Echo "[['40', 'huali Bao Elementary School in Taoxi town '], ['41', 'longling Heilongjiang Hope Primary School in Taoxi town'], ['42 ', 'letanxi Town Junior High School'], ['43 ', 'lianyuan Junior High School'] ";
Break;
Case 1:
Echo "[['40', 'huali Bao Elementary School in Taoxi town '], ['41', 'longling Heilongjiang Hope Primary School in Taoxi town']";
Break;
Case 2:
Echo "[['42', 'letanxi Town Junior High School '], ['43', 'lianyi Junior High School in Lotte Creek ']";
Break;
Default:
Break;
}
?>
Related Article

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.