Solution to how ajax transmits garbled Chinese parameters For some reason, ajax defaults to uft-8 encoding, so when we use gbk, it is easy to have garbled characters. Now we will tell you how to solve php ajax garbled characters.
Solution to how ajax transmits garbled Chinese parameters
For some reason, ajax defaults to uft-8 encoding, so when we use gbk, it is easy to have garbled characters. Let us tell you how to solve the problem of ajax garbled characters in the PHP tutorial.
Pass the Chinese parameters and then modify the database tutorial.
<script><br> var url="admin/ajaxmodify.php?"+key+"=";<br> url+=encodeuricomponent(encodeuricomponent(value));<br> xmlhttp.open("get",url,true);<br> xmlhttp.send(null);<br> </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, when using character transcoding, it is recommended to use encodeuricomponent() or encodeuri() instead of escape(). The reason is that escape() only converts ascii characters into codes such as %unnnn. If you want to use more characters such as utf-8 character library, you must use encodeuricomponent() or encodeuri() to convert. The code must be %nn%nn.
js: encodeuricomponent - decodeuricomponent; php: rawurlencode - rawurldecode
*/