Home  >  Article  >  Backend Development  >  Ajax+php Chinese garbled solution_PHP tutorial

Ajax+php Chinese garbled solution_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:17:53872browse

Since XMLHTTP uses Unicode encoding to upload data, and general pages use gb2312, this causes garbled characters when displaying the page. When getting the page, XMLHttp returns UTF-8 encoding, which causes garbled display.
One of the solutions is to display the declaration as GB2312

in the PHP file

header("Content-Type:text/html;charset=GB2312");

And transcode the Chinese sent to the server.
As follows
$_POST["content"]=iconv("UTF-8","gb2312",$_POST["content"]);
Therefore, this can solve the problem of garbled characters

Method 2 uses UTF-8 encoding. Not much to say here

Attached test routine
Client





ajax post test








Server side
header("Content-Type:text/html;charset=GB2312");
if($_POST['content'])
{
$_POST["content"]=iconv("UTF-8","gb2312",$_POST["content"]);
print("The content is".$_POST['content']);
}
else
{
print("No content sent");
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/371911.htmlTechArticleSince XMLHTTP uses Unicode encoding to upload data, and general pages use gb2312, this causes the problem when displaying the page. Generate gibberish. When getting the page, XMLHttp returns utf-8...
Statement:
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