首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Ajax >

php+ajax乱码有关问题

2012-02-03 
php+ajax乱码问题 - Web 开发 / Ajax开发环境xp+php5.2.4+mysql5.0.22+apache2.2.6编码使用gbk功能:省市三

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['provincecode']);
  $pro=iconv("utf-8","gbk",$pro);
  $sql="select * from dqb where sjdqm=$pro";


  $result=$db->query($sql);
  ?>
  <select onChange="queryArea(this.value)">
   
  <option value='-1' selected>请选择市/区</option>
  <?
  while($row=$result->fetch_assoc()){
  echo "<option value=".$row['dm'].">".$row['mc']."</option>";
  }
   
  }
?>
</select>
<?

  if($_GET['citycode']!=""){
  $cityc=urldecode($_GET['citycode']);
  $cityc=iconv("utf-8","gbk",$cityc);
  $sql="select * from dqb where sjdqm=$cityc";
  $result=$db->query($sql);
  if($result->num_rows>0)
  {
  ?>
  <select>
  <option value='-1' selected>请选择区/县</option>
  <?
  while($row=$result->fetch_assoc()){
  echo "<option value=".$row['dm'].">".$row['mc']."</option>";
  }
  echo "</select>";
  }
  }
   
?></body>
</html>
------------------------------------
conn.php
<?php
$db= new mysqli('192.168.1.10','root','123456','db_test');
if(mysqli_connect_errno())
{
  echo 'error:database connect error!';
  exit;
}
$db->query("set names gbk");
?>
但不知为什么,总是乱码,试了很多次了

[解决办法]
header("Content-type::text/html;charset=GBK");
接受页面加上试试

热点排行