ajax+php Chinese garbled solution

Source: Internet
Author: User

Because the XMLHTTP uses the Unicode code uploads the data, but the General page uses is gb2312, this causes the display page to produce garbled. And when the page is obtained when the XMLHTTP return is UTF-8 encoding, which caused the display generated garbled.
One solution is to display the declaration as GB2312 in the PHP file

Header ("content-type:text/html;charset=gb2312");

And for the Chinese to send to the server transcoding.
As follows
$_post["Content"]=iconv ("UTF-8", "gb2312", $_post["content"]);
Therefore, this can solve the garbled problem

Method Two, is all using UTF-8 encoding. There's not much to say here.

Attached test routines
Client

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"


<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "
<title>ajax post test</title>

<body>
<div id=" Msg "></div>
<script language=" javascript "
/**
* Initialize a XMLHTTP object
*/
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!= ' undefined ') {
Ajax = new XMLHttpRequest ();
}
return ajax;
}
//There is a form in the form test page, a displayed layer
function SendData ()
{
var Msg=document.getelementbyid ("msg");
var f= Document.form1;
Var c=f.content.value;
//Receive data 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.responsetext;
  }
}
}

</script>

<form name= ' Form1 ' >
<input type= "text" name= ' content ' size=10>
<input type= "button" value= "OK" onclick= "SendData ()" ><!--I made an error with submit-->
</form>
</body>

Server-side
<?php
Header ("content-type:text/html;charset=gb2312");
if ($_post[' content ')]
{
$_post["Content"]=iconv ("UTF-8", "gb2312", $_post["content"]);
Print ("Contents is". $_post[' content ')];
}
Else
{
Print ("No content to send");
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.