我制作了一个表格,用户可以填写它并检查用户是否正确插入电子邮件。'HTML文档的字符编码未声明'问题
- 如果返回false:用户会看到一条错误消息。
- 如果它返回true:它会向用户显示一条确认消息,但它也会将数据写入数据库。
因此,当我试图在PHP文件中添加表单时,发生了问题,因此我可以发出警报或在同一页上给用户提供错误消息。我开始收到此错误:
The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must to be declared in the document or in the transfer protocol.
所以我试图恢复到旧版本还原此,我不得不剪切和粘贴代码,但没有太复杂了几件。出于一些奇怪的原因,我仍然得到这个错误。当然,我在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;
}
功能sanitize方法是这样的:
function sanitize($dirty){
$clean = htmlspecialchars($dirty);
$cleaner = mysql_real_escape_string($clean);
return $cleaner;
}
你究竟在哪里看到那条消息,它是什么产生的? – deceze
我希望你没有真正将函数命名为'function'? ('函数($ _ POST [ '1']);')。这可能会弄乱一些代码,因为'function'是一个保留关键字。 – Jelmer
@deceze检查编辑。 –