Home  >  Article  >  Backend Development  >  What to do if JavaScript is garbled in Chinese in PHP

What to do if JavaScript is garbled in Chinese in PHP

coldplay.xixi
coldplay.xixiOriginal
2020-08-12 11:10:591471browse

Solution to the garbled Chinese characters of JavaScript in PHP: 1. Display the declaration as GB2312 in the PHP file, and transcode the Chinese sent to the server; 2. The codes are all encoded in [UTF-8]. Can.

What to do if JavaScript is garbled in Chinese in PHP

Solution to Chinese garbled javascript in PHP:

One of the solutions is to use PHP The file shows that the declaration is GB2312

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

and the Chinese sent to the server is transcoded.

As follows

$_POST["content"]=iconv("UTF-8","gb2312",$_POST["content"]);

So this can solve the problem of garbled characters

Related learning recommendations: javascript video tutorial

The second solution is to use UTF-8 encoding.

Attached test routine

Client

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>ajax post test</title>
</head>
<body>
<div id="msg"></div>
<script language="javascript">
/**
* 初始化一个xmlhttp对象
*/
function InitAjax()
{
 var ajax=false;
 try {
  ajax = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   ajax = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   ajax = false;
  }
 }
 if (!ajax && typeof XMLHttpRequest!=&#39;undefined&#39;) {
  ajax = new XMLHttpRequest();
 }
 return ajax;
}
//在form 测试页面内有一个表单,一个显示的层
function sendData()
{
var msg=document.getElementById("msg");
var f=document.form1;
var c=f.content.value;
//接收数据的URL
var url="dispmsg.php";
var poststr="content="+c;
var ajax=InitAjax();
ajax.open("POST",url,true);
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
ajax.send(poststr);
ajax.onreadystatechange=function(){
   if(ajax.readyState==4 && ajax.status==200){
   alert("I got something");
    msg.innerHTML=ajax.resp****eText;
   }
}
}
</script>
<form name=&#39;form1&#39;>
<input type="text" name=&#39;content&#39; size=10>
<input type="button" value="确定" οnclick="sendData()"><!--我用submit时就出错-->
</form>
</body>
</html>

Server

Related learning recommendations: php programming (video)

The above is the detailed content of What to do if JavaScript is garbled in Chinese in PHP. For more information, please follow other related articles on the PHP Chinese website!

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