Mar*_*o S 5 html php character-encoding meta-tags
我创建了一个用户可以填写的表单,它会检查用户是否正确插入了电子邮件.
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>';
}
?>
Run Code Online (Sandbox Code Playgroud)
我必须重命名变量和类,因为它们与我工作的公司有很强的关系.
编辑:此错误是由使用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';
}
Run Code Online (Sandbox Code Playgroud)
函数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;
}
Run Code Online (Sandbox Code Playgroud)
功能清理是这样的:
function sanitize($dirty){
$clean = htmlspecialchars($dirty);
$cleaner = mysql_real_escape_string($clean);
return $cleaner;
}
Run Code Online (Sandbox Code Playgroud)
经过对 PHP 聊天的一些讨论,问题似乎出在多余的分号上
从 :
mysql_query(); or die();
Run Code Online (Sandbox Code Playgroud)
到 :
mysql_query() or die();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2145 次 |
| 最近记录: |