想做个搜索的下拉列表,搜了些代码<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>Ajax Auto Suggest</title><script type="text/javascript" src="so/jquery-1.2.1.pack.js"></script>
<script type="text/javascript">
function lookup(inputString) {
if(inputString.length == 0) {
// Hide the suggestion box.
$('#suggestions').hide();
} else {
$.post("rpc.php", {queryString: ""+inputString+""}, function(data){
if(data.length >0) {
$('#suggestions').show();
$('#autoSuggestionsList').html(data);
}
});
}
} // lookup

function fill(thisValue) {
$('#inputString').val(thisValue);
setTimeout("$('#suggestions').hide();", 200);
}
</script><style type="text/css">
body {
font-family: 宋体;
font-size: 11px;
color: #000;
}

h3 {
margin: 0px;
padding: 0px;
} .suggestionsBox {
position: relative;
left: 0px;
margin: 0px 0px 0px 0px;
width: 200px;
background-color: #fff;
border: 1px solid #000;
color: #000;
}

.suggestionList {
margin: 0px;
padding: 0px;
}
.suggestionList li {
margin: 0px 0px 3px 0px;
padding: 3px;
cursor: pointer;
list-style: none;
text-align: left
}

.suggestionList li:hover {
background-color: #659CD8;
}
</style></head><body>
<div>
<form>
<div>
Type your county:
<br />
<input type="text" size="30" value="" id="inputString" onkeyup="lookup(this.value);" onblur="fill();" />
</div>
<div class="suggestionsBox" id="suggestions" style="display: none;">
<div class="suggestionList" id="autoSuggestionsList">
 
</div>
</div>
</form>
</div></body>
</html>下面是rpc的查询代码<?php
require_once(dirname(__FILE__)."/../include/common.inc.php");
 
$q=$_POST['queryString'];
 
$keyword=$q;
$oldkeyword = $keyword = FilterSearch(stripslashes($keyword));
 
$sql="SELECT keyname FROM `wwwkeyword` WHERE  binary `keyname` LIKE '%$oldkeyword%' LIMIT 0 , 5";
$result=mysql_query($sql);
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
  
   // foreach ($line as $col_value) {
   echo '<li onClick="fill(\''.$line[keyname].'\');">'.$line[keyname].'</li>';
  // }
   
}
//echo ExecTime() - $t1;
?>查询英文和数字是正常的,但输入中文就出现乱码,哪位高手看下

解决方案 »

  1.   

    在执行query 之前 执行 mysql_query("SET NAMES 'GBK'"); 
      

  2.   

    补充一下:
    还得看你数据库中的编码是不是gbk,你查看一下在数据库中乱码吗、
      

  3.   

    如果楼上的做法还不能解决问题的话,试着加入mb_convert_encoding函数
    <?php
    require_once(dirname(__FILE__)."/../include/common.inc.php");
     
    $q=$_POST['queryString'];
     
    $keyword=$q;
    $oldkeyword = $keyword = FilterSearch(stripslashes($keyword));
     
    $sql="SELECT keyname FROM `wwwkeyword` WHERE  binary `keyname` LIKE '%$oldkeyword%' LIMIT 0 , 5";
    $result=mysql_query($sql);
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
      
       // foreach ($line as $col_value) {
          echo '<li onClick="fill(\''.mb_convert_encoding($line[keyname],"UTF-8","GBK").'\');">'.$line[keyname].'</li>';
      // }
       
    }
    //echo ExecTime() - $t1;
    ?>
      

  4.   

    上面写太快了,少写了一些,试着
    <?php
    require_once(dirname(__FILE__)."/../include/common.inc.php");
     
    $q=$_POST['queryString'];
     
    $keyword=mb_convert_encoding($q,"GBK","UTF-8");
    $oldkeyword = $keyword = FilterSearch(stripslashes($keyword));
     
    $sql="SELECT keyname FROM `wwwkeyword` WHERE  binary `keyname` LIKE '%$oldkeyword%' LIMIT 0 , 5";
    $result=mysql_query($sql);
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
      $keyname=mb_convert_encoding($line[keyname],"UTF-8","GBK");
       // foreach ($line as $col_value) {
          echo '<li onClick="fill(\''.$keyname.'\');">'.$keyname.'</li>';
      // }
       
    }
    //echo ExecTime() - $t1;
    ?>没测试过,希望能帮你解决问题
      

  5.   

    $keyword=mb_convert_encoding($q,"GBK","UTF-8");我只加了这个问题就解决了,谢谢楼上的