日期:2014-05-16  浏览次数:20615 次

php+ajax乱码问题 - Web 开发 / Ajax
开发环境
xp+php5.2.4+mysql5.0.22+apache2.2.6
编码使用gbk
功能:省市三级联动
代码:
index.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<script type="text/javascript">
var xmlHttp=false;
var requestType="";

function createXMLHttpRequest()
  {
  if(window.ActiveXObject)
  {
   
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if(window.XMLHttpRequest)
  {
  xmlHttp=new XMLHttpRequest();
  }
}
function queryCity(citycode){
  createXMLHttpRequest();
  type="city";
  var url="data.php?provincecode="+encodeURI(encodeURI(citycode));
  xmlHttp.open("GET",url,true);
  xmlHttp.setRequestHeader("Content-Type","text/xml;charset=gbk;");
  xmlHttp.onreadystatechange=handleStateChange;
  xmlHttp.send(null);
}

function queryArea(citycode){ 
  createXMLHttpRequest();
  type="area";
  var url="data.php?citycode="+encodeURI(encodeURI(citycode));
  xmlHttp.open("GET",url,true);
  xmlHttp.setRequestHeader("Content-Type","text/xml;charset=gbk;");
  xmlHttp.onreadystatechange=handleStateChange;
  xmlHttp.send(null);
}

function handleStateChange(){
  if(xmlHttp.readystate==4){
  if(xmlHttp.status==200){
  if(type=="city"){
  showcity();
  }else if(type="area"){
  showarea();
  }
  }
  }
}

function showcity(){
  document.getElementById("city").innerHTML=xmlHttp.responseText;
  document.getElementById("area").innerHTML="";
}
function showarea(){
  document.getElementById("area").innerHTML=xmlHttp.responseText;
}
</script>
</head>
<body>
<?
include('conn.php');
$sql="select * from dqb where sjdqm=0";
$result=$db->query($sql);
  echo "<from id='form1'>\n";
  echo "<select id='province' onchange='queryCity(this.value)'>\n";
  echo "<option value='-1' selected>请选择省/市</option>\n";
  while($row=$result->fetch_assoc()){
  echo "<option value=".$row['dm'].">".$row['mc']."</option>\n";
  }
  echo "</select>\n";
  echo "<span id='city'></span>\n";
  echo "<span id='area'></span>\n";
  echo "</form>\n";
?>
</body>
</html>
--------------------------------------------
data.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
</head>
<body>
<?
  include("conn.php");
  if($_GET['provincecode']!=""){
  $pro=urldecode($_GET['provin