226

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 don't know where is the problem in my code. Please help me.

xxx
  • 1,153
  • 1
  • 11
  • 23
xing
  • 2,423
  • 4
  • 19
  • 11
  • 4
    Some good reading: [Declaring character encodings in HTML](http://www.w3.org/International/questions/qa-html-encoding-declarations) – Pekka Aug 16 '12 at 22:21
  • Try naming you .html page to .php: `insert.html` -> `insert.php`. And name your form handling file (`insert.php`) to `insert_action.php` or something. (Not sure if it helps but I wouldn't mix `html` and `php` pages when using PHP.) – ZZ-bb Oct 10 '12 at 13:07
  • Oh dear, this was an old question. Didn't notice the dates because of the new answer... – ZZ-bb Oct 10 '12 at 13:10
  • @xing, Which browser are you using? – Pacerier Mar 13 '15 at 13:35
  • This also works with bare HTML – Chiara Ani Jan 22 '21 at 16:56
  • You have a typo in your code. `` Should be **``** Also `` as mention down there should work for ya, as It was for me, when I had this kind of error. – Marcin Jul 30 '21 at 21:04

6 Answers6

352

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

Note: if any line between <head> and these meta line (Even if it's a commented note) problem will remain, so make sure you added meta line after <head> directly

AnasSafi
  • 5,353
  • 1
  • 35
  • 38
Krishan Gopal
  • 4,073
  • 1
  • 16
  • 19
75

I had the same problem with the most basic situation and my problem was solved by inserting this tag into the head of the document:

<meta charset="utf-8">

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

More on it here, and here.

x-yuri
  • 16,722
  • 15
  • 114
  • 161
26

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.

<!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>
Ishara Amarasekera
  • 1,387
  • 1
  • 20
  • 34
21

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>
AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
  • 1
    Thanks, I was having a similar problem, and your answer helped fixed it (I was missing the meta http-equiv... line). – David Miani Aug 19 '12 at 02:48
4

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

and add this following line

header('Content-Type: text/html; charset=utf-8');
Renjith V R
  • 2,981
  • 2
  • 22
  • 32
  • This is a better/more direct/efficient to way to set the charset, if you can use this method. – andora Apr 26 '21 at 11:57
2

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

 ?>
gkrogers
  • 8,126
  • 3
  • 29
  • 36