Ajax friends must know that JavaScript is using UTF-8 International code, that is, each Chinese character is stored in 3 bytes, but this causes the use of Ajax to send data when garbled.
One solution is to use encodeURIComponent Plus modify content-type for application/x-www-form-urlencoded "to encode the data in a URL format, but there is a disadvantage The UrlDecode using PHP simply can't get the correct text.
Another approach is to convert the data into gb2312 format by using VBScript's written function, which I personally find to be a better approach. Interested friends can go to the Internet to check.
Today's whim, the AJAX call will not send cookies? Write a program immediately to test, sure enough, so that you can invoke Ajax before invoking the data through JavaScript in the cookie, and then send the cookie can be sent out of the data, too cool!!
Demo Address: http://cn5.cn/ajax/ajax12.htm
Client code ajax.htm
Copy Code code as follows:
<title>ajax use cookies to pass the value example </title>
<script>
var odiv
var xh
function GetXML ()
{
Setcookie ($ (' name '). value,$ (' Val '). Value);
Odiv = DOCUMENT.ALL.M
odiv.innerhtml = "Loading, please wait ..."
Odiv.style.display= ""
XH = new ActiveXObject ("Microsoft.XMLHTTP")
Xh.onreadystatechange = Getready
Xh.open ("POST", "a.php", false)
Xh.send ();
}
function Getready ()
{
if (xh.readystate==4)
{
if (xh.status==200)
{
odiv.innerhtml = "complete"
}
Else
{
odiv.innerhtml = "Sorry, load data failed." Reason: "+ xh.statustext
}
}
}//author:longbill www.longbill.cn
function Setcookie (name,value)
{
var cookiestr=name+ "=" +value+ ";";
var expires = "";
var cookieexp=60*60*1000;
var d = new Date ();
D.settime (D.gettime () + cookieexp);
expires = "expires=" + d.togmtstring () + ";";
Document.cookie = cookiestr+ expires;
}
function $ (a)
{
Return document.getElementById (a);
}
</script>
<body>
Ajax uses cookies to pass the value example:<br>
<form name=myform>
Name:<input id=name value= "variable name can even be Chinese" size=20><br>
Value:<input type=text size=20 id=val value= ><br>
<input onclick= "GetXML ()" type= "button" value= "Send out Data" >
<input onclick= "if (xh && xh.responsetext) {alert (xh.responsetext);}" type= "button" value= "Show return results" >< Br>
<div id=m bgcolor=blue> This display status </div>
<input Type=button onclick= "alert (document.cookie)" value= displays local cookie>
</form>
Server-side Code a.php
Copy Code code as follows:
?
Header ("content-type:text/html;charset=gb2312");
echo "Below is all the cookie variables and their value \ n" that you send out;
Print_r ($_cookie);
?>