Solution to how to pass garbled Chinese parameters in ajax_PHP tutorial

WBOY
Release: 2016-07-13 10:54:01
Original
921 people have browsed it

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


*/

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632362.htmlTechArticleA solution to how ajax passes garbled Chinese parameters. For some reason, ajax defaults to uft-8 encoding, so we use Gbk is prone to garbled characters. Let’s tell you about php...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template