The character encoding of the HTML document was not declared
When a browser renders an HTML document, it must know how to interpret the byte stream coming from the server. Without this information, the browser defaults to a specific legacy charset, often ISO-8859-1 (Latin-1), which only covers 256 characters. If your application contains characters outside that US-ASCII rangeâlike accented letters, symbols from other scripts, or emojisâthe browser will attempt to map those bytes incorrectly, resulting in garbled text known as mojibake.
The error message The character encoding of the HTML document was not declared indicates a failure in this negotiation process. In your specific case, the issue stems from a conflict between the HTML meta tag and the HTTP response headers.
Looking at your insert.html, you have included a <meta> tag within the head section:
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
This tells the browser to treat the document as UTF-8. However, modern browsers prioritize HTTP response headers over meta tags if both are present. If your server sends a header like Content-Type: text/html without explicitly specifying charset=utf-8, the browser ignores your meta tag and falls back to its default or the protocol default.
The Role of HTTP Headers
The HTTP specification dictates that the Content-Type header is the authoritative source for character encoding. When you submit a form, the server processes the request and generates a response. If your PHP script (insert.php) or any middleware handling the response does not set the charset, the browser assumes a non-UTF-8 encoding.
To fix this at the protocol level, you must ensure the Content-Type header includes the charset parameter. In raw PHP, you would do this with:
header("Content-Type: text/html; charset=utf-8");
It is crucial to place this line as early as possible in the script, before any output is sent or echoed. Even a single character of whitespace or a BOM (Byte Order Mark) at the start of the file can cause issues if not handled correctly.
The Meta Tag Fallback
While fixing the headers is the correct approach, the meta tag you included serves as a fallback for older browsers or scenarios where server-side header manipulation isn't possible. However, relying solely on the meta tag is insufficient in modern environments because many frameworks and servers override default headers.
Your current HTML structure is otherwise valid regarding XHTML DTD declarations, though using <!DOCTYPE html> without the public ID is sufficient for HTML5 and simplifies compatibility across browsers:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- The http-equiv meta tag is less necessary if headers are correct -->
</head>
<body>
...
</body>
</html>
Handling Form Data in PHP
There is also a secondary consideration regarding how you access the form data. In your insert.php file, you are accessing $_POST["title"]. While this works in most cases, it is slightly more robust to use null coalescing or explicit defaults if the key might be missing, though that is not the primary cause of the encoding error.
The real danger here is character consistency. If your database stores data as UTF-8 and your PHP script outputs it without forcing a UTF-8 header, you risk corrupting multi-byte sequences when they are displayed in a browser expecting ISO-8859-1. This corruption happens because the byte stream for 'é' (two bytes in UTF-8) is misinterpreted as two separate single-byte characters in Latin-1.
Modern Best Practices
In modern web development, relying on http-equiv meta tags is increasingly discouraged in favor of explicit HTTP headers. If you are using a framework or a server configuration that handles headers automatically (like Apache's .htaccess or Nginx config), ensure the charset is set globally:
AddDefaultCharset UTF-8
Or in Nginx:
default_type text/html;
charset utf-8;
By ensuring the Content-Type header explicitly declares charset=utf-8, you eliminate the ambiguity that causes the browser to misrender non-ASCII characters. This approach ensures that whether a user accesses your page via HTTP/1.0 or HTTP/2, or using an older browser, the character encoding is communicated reliably before the document content is parsed.