How to transmit garbled Chinese parameters in ajax

Source: Internet
Author: User

Pass Chinese parameters, and then modify the database tutorial.

The code is as follows: Copy code

<Script>
Var url = "admin/ajaxmodify. php? "+ Key +" = ";
Url + = encodeuricomponent (value ));
Xmlhttp. open ("get", url, true );
Xmlhttp. send (null );
</Script>
The parameter is Chinese. encodeuricomponent. This method must be called twice.
*/

Function utf8rawurldecode ($ source ){
$ Decodedstr = "";
$ Pos = 0;
$ Len = strlen ($ source );
While ($ pos <$ len ){
$ Charat = substr ($ source, $ pos, 1 );
If ($ charat = '% '){
$ Pos ++;
$ Charat = substr ($ source, $ pos, 1 );
If ($ charat = 'u '){
// We got a unicode character
$ Pos ++;
$ Unicodehexval = substr ($ source, $ pos, 4 );
$ Unicode = hexdec ($ unicodehexval );
$ Entity = "& #". $ unicode .';';
$ Decodedstr. = utf8_encode ($ entity );
$ Pos + = 4;
  }
Else {
// We have an escaped ascii character
$ Hexval = substr ($ source, $ pos, 2 );
$ Decodedstr. = chr (hexdec ($ hexval ));
$ Pos + = 2;
  }
} Else {
$ Decodedstr. = $ charat;
$ Pos ++;
  }
  }
Return $ decodedstr;
}


/*
Note: In js, encodeuricomponent () or encodeuri () is recommended for character transcoding, rather than escape (). The reason is that escape () only converts ascii characters to % unnnn. If you want to use more characters, such as the UTF-8 character library, you must use encodeuricomponent () or encodeuri () can be converted to % nn.

Js: encodeuricomponent -- decodeuricomponent; php: rawurlencode -- rawurldecode


*/

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.