[php] The character encoding of the HTML document was not declared

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

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

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

This question is related to php html

The answer is


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

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

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


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

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

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

 ?>

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.php file:

<!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>

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.

_x000D_
_x000D_
<!DOCTYPE html>_x000D_
<html>_x000D_
_x000D_
<head>_x000D_
  <meta charset="utf-8" />_x000D_
  <title>Voice clip upload</title>_x000D_
  <script src="voiceclip.js"></script>_x000D_
</head>_x000D_
_x000D_
<body>_x000D_
  <h2>Upload Voice Clip</h2>_x000D_
  <form id="upload_form" enctype="multipart/form-data" method="post">_x000D_
    <input type="file" name="file1" id="file1" onchange="uploadFile()"><br>_x000D_
    <progress id="progressBar" value="0" max="100" style="width:300px;"></progress>_x000D_
  </form>_x000D_
</body>_x000D_
_x000D_
</html>
_x000D_
_x000D_
_x000D_


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

and add this following line

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