繁体   English   中英

'HTML文档的字符编码未声明'问题

[英]'The character encoding of the HTML document was not declared' issues

我创建了一个用户可以填写的表单,它会检查用户是否正确插入了电子邮件。

  • 如果返回false:用户将看到错误消息。
  • 如果返回true:它会显示用户的确认消息,但它也将INSERT数据到数据库中。

因此,当我尝试在PHP文件中添加表单时出现问题,因此我可以发出警报或在同一页面上向用户提供错误消息。 我开始得到这个错误:

未声明HTML文档的字符编码。 如果文档包含US-ASCII范围之外的字符,则文档将在某些浏览器配置中使用乱码文本进行渲染。 必须在文档或传输协议中声明页面的字符编码。

所以我试图通过恢复到旧版本来恢复它,我不得不剪切和粘贴几段代码,但没有太复杂。 由于一些奇怪的原因,我仍然得到这个错误。 当然,我在Stackoverflow上检查了这个问题的相似主题,但没有一个答案让我更进一步。 例如,在页面的标题中添加meta信息,我还创建了一个这样的PHP标题: header('Content-type: text/html; charset=utf-8'); 之后我读了一些关于这个错误的文章,但没有人帮我解决这个问题。

这是我的代码示例。 这是我收到错误的页面:

 <?php    
include_once('credentials.php');
mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
include_once('functions.php');

 $apples = sanitize($_POST['Apple']);
 $oranges = sanitize($_POST['Orange']);
 $melons = sanitize($_POST['Melon']);
 $pears = sanitize($_POST['Pear']);
 $strawberries = sanitize($_POST['Strawberry']);
 $grapes = sanitize($_POST['Grape']);               
    if($_POST['Grape'] == 'Yes')
    { $grapes = 1; }
    else
    { $grapes = 0; } 
$blueberries = sanitize($_POST['Blueberry']);

if (checkStrawberries($strawberries) == true) {    
    mysql_query("   INSERT INTO tablename (Name, Weight, Color, Ripeness, Age, Origin, Destination) 
            VALUES ('$apples', '$oranges', '$melons', '$Pears', '$strawberries', '$grapes', '$blueberries') ");

    or die(mysql_error()); 

    echo '<!DOCTYPE html>   
            <html>
                <head>
                    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
                </head>
                <title>Page</title>
                <body>
                    True
                    <a href="../index.php">home</a>
                </body>
            </html>';
} else {
    echo '<!DOCTYPE html>   
            <html>
                <head>
                    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
                </head>
                <title>Pagina</title>
                <body>
                    False
                    <a href="../index.php">home</a>
                </body>
            </html>';
}
?>

我必须重命名变量和类,因为它们与我工作的公司有很强的关系。

编辑 :此错误是由使用Firebug的Firefox中的控制台日志生成的。

编辑2 :更改了var和函数名称以避免混淆并说明此应用程序中的数据交换。

编辑3 :我试图找出我的代码中究竟是什么产生了这个错误,因为我测试了编码部分,下面由Dobromir Velev提供给我的测试用例,并且工作得很好。 我决定将IF ELSE语句注释掉,但我没有收到错误。 所以这似乎破坏了页面。

我暂时删除了回声,因为它没有为这个特定挑战增加任何价值。

if (checkStrawberries($strawberries) == true) {    
    mysql_query("INSERT INTO tablename (Name, Weight, Color, Ripeness, Age, Origin, Destination) 
    VALUES ('$apples', '$oranges', '$melons', '$Pears', '$strawberries', '$grapes', '$blueberries') ");
    or die(mysql_error());
} else {
    echo 'FALSE';
}

函数checkStrawberries是这样的:

function checkEmail($email) {
if (!preg_match("/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\._-] +)+$/" , $email)) {
return false;
}
return true;
}

功能清理是这样的:

function sanitize($dirty){
$clean = htmlspecialchars($dirty);
$cleaner = mysql_real_escape_string($clean);
return $cleaner;
}

我首先尝试使用HTTP标头和元标记的文件。

<?php
header("Content-Type: text/html; charset=utf-8");
?>

<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
</head>
<body>
text
</body>
</html>

如果这不会触发错误,那么它可能是您代码中的内容。 例如, <?php标记之前的空格将阻止PHP设置HTTP标头。 此外,如果包含文件file1.php或file2.php返回任何内容,则可能导致浏览器错误地检测到编码

在对PHP聊天进行一些讨论后,似乎问题来自额外的分号

来自:

mysql_query(); or die();

至 :

mysql_query() or die();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM