php 未声明 HTML 文档的字符编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11996257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:36:53  来源:igfitidea点击:

The character encoding of the HTML document was not declared

phphtml

提问by xing

When I click on my form's submit button the following error message appears:

当我单击表单的提交按钮时,出现以下错误消息:

"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."

“未声明 HTML 文档的字符编码。如果文档包含来自 US-ASCII 范围之外的字符,该文档将在某些浏览器配置中呈现为乱码。必须在文档中声明页面的字符编码或在传输协议中。”

insert.html:

插入.html:

<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>insert page</title></head>
<body>
<h1> Insert Page </h1>
    <form   action="insert.php"  method="post"  enctype="application/x-www-form-urlencoded" >
    <p>Title:<input type="text"  name="title" size="40"/></p>
     <p>Price:<input type= "text"  name="price" size="40" /></p>
     <p><input type="submit" value="Insert" />
    <input type="reset"  value="Reset" /></p>
    </form>    
</body>
</html>

insert.php:

插入.php:

<?php
   $title = $_POST["title"];
   $price = $_POST["price"];

  echo $title;
 ?>

I dont know where is the problem in my code. Please help me.

我不知道我的代码哪里出了问题。请帮我。

回答by Krishan Gopal

Add this as a first line in the HEAD section of your HTML template

将此添加为 HTML 模板的 HEAD 部分的第一行

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

回答by Dragan Sekuloski

I have same problem with the most basic situation and my problem was solved with inserting this meta in the head:

我在最基本的情况下也有同样的问题,我的问题是通过在头部插入这个元来解决的:

<meta charset="UTF-8">

the character encoding (which is actually UTF-8) of the html document was not declared

未声明 html 文档的字符编码(实际上是 UTF-8)

回答by AlienWebguy

Well when you post, the browser only outputs $title- all your HTML tags and doctype go away. You need to include those in your insert.phpfile:

好吧,当您发布时,浏览器只会输出$title——您所有的 HTML 标签和文档类型都会消失。你需要在你的insert.php文件中包含这些:

<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>insert page</title></head>
<body>
<?php 

   $title = $_POST["title"];
   $price = $_POST["price"];

  echo $title;


 ?>  
</body>
</html>

回答by Ishara Amarasekera

I had the same problem when I ran my form application in Firefox. Adding <meta charset="utf-8"/>in the html code solved my issue in Firefox.

我在 Firefox 中运行表单应用程序时遇到了同样的问题。添加<meta charset="utf-8"/>html 代码解决了我在 Firefox 中的问题。

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <title>Voice clip upload</title>
  <script src="voiceclip.js"></script>
</head>

<body>
  <h2>Upload Voice Clip</h2>
  <form id="upload_form" enctype="multipart/form-data" method="post">
    <input type="file" name="file1" id="file1" onchange="uploadFile()"><br>
    <progress id="progressBar" value="0" max="100" style="width:300px;"></progress>
  </form>
</body>

</html>

回答by Renjith V R

You have to change the file from .html to .php.

您必须将文件从 .html 更改为 .php。

and add this following line

并添加以下行

header('Content-Type: text/html; charset=utf-8');

回答by gkrogers

Your initial page is a complete HTML page containing a form, the contents of which are posted to insert.php when the submit button is clicked, but insert.php needs to process the form's contents and do something with them, like add them to a database, or output them to a new page. Your current insert.php just outputs the contents of the title field, so your browser tries to interpret that as an HTML page, and fails, obviously, because it isn't valid HTML (i.e. it isn't contained in an 'HTML' tag, etc.).

您的初始页面是一个完整的 HTML 页面,其中包含一个表单,当单击提交按钮时,表单的内容将发布到 insert.php,但 insert.php 需要处理表单的内容并对其进行处理,例如将它们添加到数据库,或将它们输出到新页面。您当前的 insert.php 仅输出标题字段的内容,因此您的浏览器会尝试将其解释为 HTML 页面,但显然失败了,因为它不是有效的 HTML(即它不包含在“HTML”中)标签等)。

Your insert.php needs to output the necessary HTML, and insert the form data in there somewhere.

您的 insert.php 需要输出必要的 HTML,并将表单数据插入其中的某处。

For example:

例如:

<?php 

   $title = $_POST["title"];
   $price = $_POST["price"];

  echo '<html xmlns="http://www.w3.org/1999/xhtml">';
  echo '<head>';
  echo '<meta http-equiv="content-type" content="text/html; charset=utf-8" />';
  echo '<title>';
  echo $title;
  echo '</title>';
  echo '</head>';
  echo '<body>';
  echo 'Hello, world.';
  echo '</body>';

 ?>