开发环境
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");
?>
但不知为什么,总是乱码,试了很多次了

解决方案 »

  1.   

    看起来代码编码设置没什么问题 看看数据库中是不是都GBK编码
      

  2.   

    数据库也是,在安装时编码就设置为gbk
      

  3.   

    $cityc=iconv("utf-8","gbk",$cityc);
    $pro=iconv("utf-8","gbk",$pro);
    换成
    $cityc=iconv("utf-8","gbk//IGNORE",$cityc);
    $pro=iconv("utf-8","gbk//IGNORE",$pro);
      

  4.   

    还是不行
    我发现,因为数据库中'dm'和'sjdqm'都是数字,其中'mc'才是中文,所以传到服务端数字是不是gbk码不要紧,主要是传回来的数据,如何变成gbk
      

  5.   

    在PHP文件中加入
    header('Content-type: text/html;charset=GBK');
      

  6.   

    这是不行的,加上后程序还出错了
    Warning: Cannot modify header information - headers already sent by (output started at D:\wwwroot\testssx\index1.php:6) in
      

  7.   

    好了谢谢,,解决这个错误就好了
    把output_buffering = off改为output_buffering = on
      

  8.   

    LZ!!
    你这跟FF不兼容啊!!咋办哇!!