How to Correctly Handle PHP Ajax Garbled Code_PHP Tutorial

WBOY
Release: 2016-07-15 13:31:49
Original
812 people have browsed it

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 to PHP Ajax garbled code is to display the declaration as GB2312 in the PHP file

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

Transcode the Chinese sent to the server.

As follows

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

So this is You can solve the problem of PHP Ajax garbled characters

The second method is to use UTF-8 encoding. Not much to say here

PHP Ajax garbled code resolution example on the client side

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

PHP Ajax garbled code resolution example on the server side

<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php   </span></span></li><li><span>header("Content-Type:text<br />/html;</span><span class="attribute">charset</span><span>=</span><span class="attribute-value">GB2312</span><span>");   </span></li><li class="alt"><span>if($_POST['content'])   </span></li><li><span>{   </span></li><li class="alt"><span>$_POST["content"]=iconv("<br />UTF-8","gb2312",$_POST["content"]);   </span></li><li><span>print("内容是".$_POST['content']);   </span></li><li class="alt"><span>}   </span></li><li><span>else   </span></li><li class="alt"><span>{   </span></li><li><span>print("没有内容发送");   </span></li><li class="alt"><span>}   </span></li><li><span class="tag">?></span><span>  </span></span></li></ol>
Copy after login

The above code examples are related solutions to PHP Ajax garbled code. I hope it will be helpful to friends who need it again.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446195.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...
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