1

我对 $_POST 的字符集有疑问。当我提交表单时,如果 InputText 上插入的字符串有特殊字符或重音符号,则 $_POST 数组上的此输入值因无效字符而损坏。

示例:我在输入上插入:“pão” $_POST 显示:Array ([input] => pão)

我正在使用带有 ISO-8859-1 字符集的 CodeIgniter 框架。为了改进我的测试,我使用了 mb_detect_encoding() 并且这个函数返回了 utf-8。:\

下面是重要部分的代码:

/*
|--------------------------------------------------------------------------
| Default Character Set
|--------------------------------------------------------------------------
|
| This determines which character set is used by default in various methods
| that require a character set to be provided.
|
*/
$config['charset'] = "iso-8859-1";

/*
|--------------------------------------------------------------------------
| Default Language
|--------------------------------------------------------------------------
|
| This determines which set of language files should be used. Make sure
| there is an available translation if you intend to use something other
| than english.
|
*/
$config['language'] = "portugues";

$db['default']['char_set'] = "latin1";
$db['default']['dbcollat'] = "latin1_swedish_ci"; 

Form that was submited:
<form action="HTTP://localhost/portalsibe/index.php/grupos/cadastro" id="form" accept-charset="utf8" method="POST" name="frmPadrao" target="" enctype="multipart/form-data">
4

3 回答 3

2

试试这个解决方案,在你的脚本中插入这个 jquery 函数:

字体

致谢:Javier Poo,WeLinux SA Oficina:02-372.97.70,Celular:84039925 Bombero Ossa # 1010,圣地亚哥 www.welinux.cl

jQuery.fn.extend({
param: function( a ) { 
    var s = []; 

    // If an array was passed in, assume that it is an array 
    // of form elements 
    if ( a.constructor == Array || a.jquery ){
        // Serialize the form elements 
        jQuery.each( a, function(){ 
            s.push(unescape(encodeURIComponent(escape(this.name))) + "=" + unescape(encodeURIComponent(escape(this.value)))); 
        }); 
    } 
    // Otherwise, assume that it's an object of key/value pairs 
    else{ 
        // Serialize the key/values 
        for ( var j in a ) 
            // If the value is an array then the key names need to be repeated 
            if ( a[j] && a[j].constructor == Array ) 
                jQuery.each( a[j], function(){ 
                    s.push(unescape(encodeURIComponent(escape(j)) + "=" + encodeURIComponent(escape(this)))); 
                }); 
            else 
                s.push(unescape(encodeURIComponent(escape(j)) + "=" + encodeURIComponent(escape(a[j])))); 
    } 
    // Return the resulting serialization 
    return s.join("&").replace(/ /g, "+"); 
},

serialize: function() { 
    return this.param(this.serializeArray()); 
}
});
于 2013-10-03T13:38:54.937 回答
0

然后你需要“转换”你所有的文件和数据库做 ISO-8859-1。不要忘记在您的 PHP/HTML 文件中添加编码。

例如,在 HTML4:<meta http-equiv="Content-type" content="text/html;charset=ISO-8859-1">和 HTML5 中:<meta charset="ISO-8859-1">

<form>另外,尝试将标签中的 enctype 更改为application/x-www-form-urlencoded,看看它是否有效。

于 2013-09-01T18:50:40.377 回答
0

你能把所有东西都改成utf8吗?包括数据库?

如果是,则更改所有文件,并将 MySQL 数据库(和表)设置为utf8_general_ci. 如果您使用 notepad++ 进行开发,请转到Encoding > Encode in UTF-8(em português Formatar > Codificação em UTF-8)。

尽量不要使用 ISO-8859-1 作为字符编码。

于 2013-08-30T19:45:47.843 回答