有两个表单页,一个是gb2312编码,另一个是utf8编码,都提交到utf8编码的search.php页面。
怎样才能正确处理这两个表单传来的数据而不乱码?
form.cn.php<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>表单GB2312</title></head>
<body><form action="search.php" method="get" enctype="application/x-www-form-urlencoded">
<input name="lng" type="hidden" value="cn">
<input name="kw" type="text" value="中文">
<input name="t" type="hidden" value="<?php echo rand(1,1000); ?>">
<input type="submit" value="Send">
</form></body>
</html>form.un.php<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>表单UTF8</title></head>
<body><form action="search.php" method="get" enctype="application/x-www-form-urlencoded">
<input name="lng" type="hidden" value="un">
<input name="kw" type="text" value="中文">
<input name="t" type="hidden" value="<?php echo rand(100,1000); ?>">
<input type="submit" value="Send">
</form></body>
</html>search.php<?php
$lng=$_REQUEST['lng'];
$kw=$_REQUEST['kw'];
$cs=($lng=='cn')?'GB2312':'UTF-8';
$ct="Content-type: text/html; charset=".$cs;
header($ct);
?>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=<?php echo $cs; ?>">
<title>Document</title></head>
<body><a href="form.un.php">UTF8</a>
<a href="form.cn.php">GB2312</a>
<?php
// 处理表单数据
// 现在的情况是UTF8有显示,GB2312无显示
echo $kw;
?></body>
</html>

解决方案 »

  1.   

    转码呗,如果不是utf-8则转换再查询。
      

  2.   

    把search.php页的title改了一下:<title><?php echo '处理'.$cs; ?></title>GB2312下貌似‘处理’二字乱码呢!
    怎么回事?
      

  3.   

    查看源代码竟然还是UTF-8格式!错在哪了呢?
      

  4.   

    首先看看$cs是什么值。然后改改你的程序的编码。改为和$cs一样就可以了。
      

  5.   

    请试下mb_convert_encoding() 函数
    作用:转换字符串的编码。
    string mb_convert_encoding ( string str, string to-encoding [, mixed from-encoding])需要enable mbstring 扩展库,mb_convert_encoding 根据内容自动识别编码,这样不管来是从哪种编码来的都可以转换成utf-8编码
      

  6.   

    刚刚把search.php改成ANSI编码,还是不行呢。<?php
    $lng=$_REQUEST['lng'];
    $kw=$_REQUEST['kw'];
    $cs=($lng=='cn')?'GB2312':'UTF-8';
    $ch="Content-type: text/html; charset=".$cs;
    header($ch);
    ?>
    <html>
    <head><meta http-equiv="Content-Type" content="text/html; charset=<?php echo $cs; ?>">
    <title><?php echo '处理'.$cs; ?></title></head>
    <body><a href="form.un.php">UTF8</a>
    <a href="form.cn.php">GB2312</a>
    <div>处理结果:</div>
    <?php
    // 处理表单数据
    // 现在的情况是UTF8有显示,GB2312无显示
    echo $kw;
    ?></body>
    </html>
      

  7.   

    这个试过了,没用。
    给你两个函数,你帮我调试下?function utf($text)
    {
    $decode=mb_detect_encoding($text,"ASCII,UTF-8,CP936,EUC-CN,BIG-5,EUC-TW"); 
    $cache=mb_convert_encoding($text,"UTF-8",$decode);
    return $cache;
    }function gb($text)
    {
    $decode=mb_detect_encoding($text,"ASCII,UTF-8,CP936,EUC-CN,BIG-5,EUC-TW"); 
    $cache=mb_convert_encoding($text,"CP936",$decode);
    return $cache;
    }
      

  8.   

    单纯修改meta中的编码是没有用的,要看文件的真实编码。
    看手册 string mb_convert_encoding ( string str, string to-encoding [, mixed from-encoding]) 中 from-encoding是可以用"auto"的,不过我试了好像对中文无效。既然楼主知道是从哪个编码过来的,那就简单了$kw = mb_convert_encoding($_REQUEST['kw'], 'utf-8', $cs);
    这样的话search.php还用utf-8编码,肯定好使
      

  9.   

    $kw = $cs=='UTF-8' ? $_REQUEST['kw'] : mb_convert_encoding($_REQUEST['kw'], 'utf-8', $cs);
    这样效率会更高一些吧
      

  10.   

    看了下表单发送的数据
    GB2312表单是:
    ?lng=cn&kw=%D6%D0%CE%C4
    UTF-8表单是:
    ?lng=un&kw=%E4%B8%AD%E6%96%87
    关键就是在于怎么把&kw=后面的
    这个"%D6%D0%CE%C4"转换成UTF8啊!
      

  11.   

    http://115.com/file/clq858wp#index.rar下载试下,我这个不知道来源编码,你的知道来源,更简单
      

  12.   

    注意改下form的action地址,我用的是POST,你的GET原理完全一样
      

  13.   


    $str='%D6%D0%CE%C4';
    echo mb_convert_encoding(urldecode($str),"utf-8","gb2312");中文谁要你用get提交,改用post提交就免了中文乱码的问题。
      

  14.   

    谢谢!下载测试了,
    来源编码=utf-8
    content=中文测试结束来源编码=gb2312
    content=测试结束gb2312时还是取不到kw的数据
      

  15.   

    方便的话,把你的文件发过来我看下。 [email protected]
      

  16.   

    Search页面保存为UTF8是带BOM的,和这个有无关系呢?
      

  17.   

    <?php
    $lng=$_REQUEST['lng'];
    $cs=($lng=='cn')?'GB2312':'UTF-8';
    //$ch="Content-type: text/html; charset=".$cs;function utf8($text)
    {
        $decode=mb_detect_encoding($text,"ASCII,UTF-8,CP936,EUC-CN,BIG-5,EUC-TW"); 
        $cache=mb_convert_encoding($text,"UTF-8",$decode);
        return $cache;
    } //header($ch);
    $kw=utf8($_REQUEST['kw']);
    ?>
    <html>
    <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title><?php echo '处理'.$cs; ?></title></head>
    <body><a href="form.un.php">UTF8</a>
    <a href="form.cn.php">GB2312</a>
    <div></div>
    <?php
    echo utf8($kw);
    ?></body>
    </html>把index.php改成这样就行了,index.php不需要动态设置charset
      

  18.   

    php如果采用utf-8编码,保存时最好不要带bom,否则如果程序中用到header,会报错
    但对楼主此贴提的问题没有影响
      

  19.   

    郁闷到极点了,还是不行啊!<?php
        $lng=$_REQUEST['lng'];
        $cs=($lng=='cn')?'GB2312':'UTF-8';function utf8($text)
    {
        $decode=mb_detect_encoding($text,"ASCII,UTF-8,CP936,EUC-CN,BIG-5,EUC-TW"); 
        $cache=mb_convert_encoding($text,"UTF-8",$decode);
        return $cache;
    }
    ?>
    <html>
    <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title><?php echo '处理'.$cs; ?></title></head>
    <body><a href="form.un.php">UTF8</a>
    <a href="form.cn.php">GB2312</a><div><?php
    $kw=utf8(urldecode($_REQUEST['kw']));
    echo $kw;
    ?></div></body>
    </html>
      

  20.   


    不是吧?我把上面的代码直接复制到index.php中,没问题啊,两种编码都可以正常取值
      

  21.   

    现在的情况是获取不到传来的kw的值,如果有乱码倒好办了。 $kw=$_REQUEST['kw'];
    var_dump($kw);显示为空
    string(0) "" 
      

  22.   

    确认已经传递了 kw
    另外将 $_REQUEST 修改成 POST 或者GET 看看究竟是什么情况.
      

  23.   

    参数确实是传递过来了,这点不用怀疑:
    http://127.0.0.1/web/form/?lng=cn&kw=%D6%D0%CE%C4
    而且修改POST 或者GET结果都一样
      

  24.   

    整理一下代码。
    这是表单GB2312表单:<form action="./" method="POST" enctype="application/x-www-form-urlencoded">
    <input name="lng" type="hidden" value="cn">
    <input name="kw" type="text" value="中文">
    <input name="t" type="hidden" value="<?php echo rand(100,1000); ?>">
    <input type="submit" value="Send">
    </form>这是UTF8处理页面:<?php
        @$lng=$_POST['lng'];
        @$cs=($lng=='cn')?'GB2312':'UTF-8';function utf8($text)
    {
        $decode=mb_detect_encoding($text,"ASCII,UTF-8,CP936,EUC-CN,BIG-5,EUC-TW"); 
        $cache=mb_convert_encoding($text,"UTF-8",$decode);
        return $cache;
    }
    ?>
    <html>
    <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title><?php echo '处理'.$cs; ?></title></head>
    <body><a href="form.un.php">UTF8</a>
    <a href="form.cn.php">GB2312</a><div><?php
    $kw=$_POST['kw'];
    var_dump($kw);
    ?></div></body>
    </html>现在只要求var_dump($kw)结果不为空就万事大吉了!
      

  25.   


    <?php
    @$lng=$_POST['lng'];
    @$cs=($lng=='cn')?'GB2312':'UTF-8';
    ?>
    <html>
    <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title><?php echo '处理'.$cs; ?></title></head>
    <body><a href="form.un.php">UTF8</a>
    <a href="form.cn.php">GB2312</a><div><?php
    $kw=$_POST['kw'];
    var_dump($kw);
    ?></div></body>
    </html>把用不到的代码都删了,还是看不出哪里错了啊!
      

  26.   

    <form action="./" method="POST" enctype="application/x-www-form-urlencoded">你确认这样的传递地址没有错吗?能直接传递到search.php ?
    用你的写轮眼还是万花筒看一下btw 不管对和错,哥要去吃饭了...
      

  27.   

    刚刚看到呢,字母和数字能够正常读取数值,
    关于action这个没问题,早就改成默认的index.php了。
    --也就是说传递中文字符时失败了!
    似乎问题范围缩小了呢!
      

  28.   

    刚刚猛然醒悟:是不是字符集设置问题。
    修改Apache配置,
    在web目录添加了的DefaultCharSet,
    问题竟然就解决了!
    <Directory "...">
    ...
        AddDefaultCharSet UTF-8
        IndexOptions Charset=UTF-8
    ...
    </Directory>
    多谢各位给了我这么多提示!
      

  29.   

    我从来没有设置过什么APACHE的字符集也没出现过任何问题。你真是悲催
      

  30.   

    上午又琢磨了下,有了新的解决办法。
    客户端表单数据都采用js脚本提交,
    在提交前使用JS的encodeURI函数先进行URL编码,
    处理页面使用PHP的urldecode解码。
    这样就避免了提交中文字符,
    同时又避免了不同浏览器的编码兼容错误,
    服务器的字符集设置也可以取消了。
    测试了一下,效果不错。:)表单页主要代码:
    <script type="text/javascript">
    function doSubmit(form)
    {
    form.kw.value=encodeURI(form.kw.value);
    form.submit();
    }
    </script>
    ......
    <input type="button" value="Send" onClick="doSubmit(this.form);">
    处理页主要代码:
    <?php
    $kw=$_GET['kw'];
    echo urldecode($kw);
    ?>