Ajax how to pass Chinese parameters garbled solution for some reason ajax by default is the uft-8 encoding, so we use a gbk is prone to garbled problem, the following describes how to solve php ajax garbled code.
How to transmit garbled Chinese parameters in ajax
For some reason, ajax is the encoding of the uft-8 by default, so we use a gbk will be prone to garbled problem, we will tell you the php tutorial ajax garbled solution.
Pass Chinese parameters, and then modify the database tutorial.
<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
*/