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

没有声明HTML文档的字符编码。如果文档包含US-ASCII范围之外的字符,那么在某些浏览器配置中,文档将以乱码文本呈现。页的字符编码必须在文档或传输协议中声明。

insert.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
    $title = $_POST["title"];
    $price = $_POST["price"];

    echo $title;
?>

我不知道我的代码有什么问题。请帮帮我。


当前回答

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

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

例如:

<?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>';

 ?>

其他回答

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

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

例如:

<?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>';

 ?>

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

<meta charset="utf-8">

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

这里还有这里。

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

加上下面这一行

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

当你发布时,浏览器只输出$title -所有的HTML标签和doctype都消失了。你需要在你的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>

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

<!DOCTYPE html > < html > < >头 <meta charset="utf-8" /> <title>语音片段上传</title> . txt < script src = " voiceclip.js " > < /脚本> < / >头 身体< > <h2>上传语音片段</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;" > < > /进展 > < /形式 身体< / > < / html >