javascript在php中文亂碼的解決方法:1、在php文件中顯示聲明為gb2312,并對(duì)于發(fā)送到服務(wù)器的中文進(jìn)行轉(zhuǎn)碼;2、代碼是都采用【utf-8】編碼即可。
javascript在php中文亂碼的解決方法:
解決方法之一,就是在php文件中顯示聲明為gb2312
header("content-type:text/html;charset=gb2312");而對(duì)于發(fā)送到服務(wù)器的中文進(jìn)行轉(zhuǎn)碼。
如下
$_post["content"]=iconv("utf-8","gb2312",$_post["content"]);因而這樣可以解決亂碼問(wèn)題
相關(guān)學(xué)習(xí)推薦:javascript視頻教程
解決方法之二,是都采用utf-8編碼。
附測(cè)試?yán)?br>客戶端
<!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">/* 初始化一個(gè)xmlhttp對(duì)象*/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;}//在form 測(cè)試頁(yè)面內(nèi)有一個(gè)表單,一個(gè)顯示的層function senddata(){var msg=document.getelementbyid("msg");var f=document.form1;var c=f.content.value;//接收數(shù)據(jù)的urlvar 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.respetext; }}}</script><form name='form1'><input type="text" name='content' size=10><input type="button" value="確定" οclick="senddata()"><!--我用submit時(shí)就出錯(cuò)--></form></body></html>服務(wù)器端
<?phpheader("content-type:text/html;charset=gb2312");if($_post['content']){$_post["content"]=iconv("utf-8","gb2312",$_post["content"]);print("內(nèi)容是".$_post['content']);}else{print("沒(méi)有內(nèi)容發(fā)送");}?>相關(guān)學(xué)習(xí)推薦:php編程(視頻)