Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    New Coder
    Join Date
    Feb 2014
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    The character encoding was not declared.

    Hi,

    I use a form in a html to submit data to the database via another php file. When the "submit" button in the form is clicked, the data in the form will be posted to a php file and the php file will insert the data to the database. I got a message:
    "The character encoding of the plain text 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. insertDB.php"
    . I check the database, the data in the form are inserted to the database. I already put
    Code:
    [HTML]<meta http-equiv="Content-Type" Content="text/html;charset=utf-8"/> <meta content="utf-8" http-equiv="encoding">[/HTML]
    in the html which includes the submitting form.

    Can someone give any suggestions? Thanks in advance!

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    what does this message refer to, the incoming form data or the outgoing HTML data?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    New Coder
    Join Date
    Feb 2014
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    what does this message refer to, the incoming form data or the outgoing HTML data?
    The message refers to the outgoing HTML data. The error message is "The character encoding was not declared..... insertDB.php", which insertDB.php is the php file for saving the data.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,627
    Thanks
    0
    Thanked 648 Times in 638 Posts
    You can't use HTML to specify the character encoding for a plain text document.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    New Coder
    Join Date
    Feb 2014
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    You can't use HTML to specify the character encoding for a plain text document.
    I don't know if I understand you right.
    Do you mean, I need to change
    <html>
    <head>
    <meta http-equiv="Content-Type'" content="text/html; charset=utf-8"/>
    <meta content="utf-8" http-equiv="encoding">
    to
    <html>
    <head>
    <meta http-equiv="Content-Type'" content="text/plain; charset=utf-8"/>
    <meta content="utf-8" http-equiv="encoding">
    .

    I tried it. But the error message still appears. Maybe I missed something. Please point out my misunderstanding. Thanks!
    Last edited by jianmarf; 05-11-2014 at 12:46 PM.

  • #6
    Regular Coder hdewantara's Avatar
    Join Date
    Aug 2009
    Location
    Jakarta, Indonesia.
    Posts
    349
    Thanks
    9
    Thanked 51 Times in 50 Posts
    Hi.
    If you get that long message after form submission, then perhaps it's your insertDB.php causing it. So maybe it doesn't just save data? It has printed out something back to browser.

    Hendra
    Last edited by hdewantara; 05-11-2014 at 03:41 PM.

  • #7
    New Coder
    Join Date
    Feb 2014
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi,

    Thanks for the reply. The error message is like "The character encoding was not declared ... insertDB.php". I thought about whether the insertDB.php causes the error. I added
    header('Content-Type: text/html; charset=utf-8");
    in the insertDB.php, but the error message still appears. I use XMLHttpRequest to post the form data to the insertDB.php. And the html only gets the response code from insertDB.php.
    if (responseCode == 200) {
    document.getElementById("message").innerHTML = "ok";
    }

  • #8
    Regular Coder hdewantara's Avatar
    Join Date
    Aug 2009
    Location
    Jakarta, Indonesia.
    Posts
    349
    Thanks
    9
    Thanked 51 Times in 50 Posts
    Well,
    If insertDB.php isn't supposed to print out anything then it doesn't actually need that header.

    My final guess, maybe the actual character-encoding of file is not UTF-8. How do you usually edit this insertDB.php file... with Notepad++? Are you sure you have saved it as UTF-8 file?
    Last edited by hdewantara; 05-11-2014 at 07:47 PM. Reason: minor edit

  • #9
    New Coder
    Join Date
    Feb 2014
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks very much for the reply. I edit and save the files in vi. I assume that vi supports utf-8 format. And I will check it up to make sure.

  • #10
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,300
    Thanks
    28
    Thanked 275 Times in 269 Posts
    Quote Originally Posted by jianmarf View Post
    I already put
    Code:
    [HTML]<meta http-equiv="Content-Type" Content="text/html;charset=utf-8"/> <meta content="utf-8" http-equiv="encoding">[/HTML]
    in the html which includes the submitting form.
    <meta content="utf-8" http-equiv="encoding"> isn't defined in any specification and is therefore meaningless. You should remove that.

    <meta http-equiv="Content-Type" Content="text/html;charset=utf-8"/> is valid, but the preferred (and shorter) form is <meta charset="utf-8">.

    Make sure the encoding meta element is the first element appearing within your head element; if it doesn't occur within the first 1024 bytes of the output code, it'll get ignored.

    Make sure your HTTP content-type header matches the meta element.

    Lastly, make sure your declared encoding matches the actual encoding of the document.

    Quote Originally Posted by jianmarf View Post
    Can someone give any suggestions? Thanks in advance!
    My guess is that there's something wrong with the data you're submitting; I can't think of any other explanation for the "plain text" part of the error message.

    I'd open your browser's network inspector (accessed via F12, then a click on the "Network" tab in Chrome; Ctrl+Shift+Q in Firefox; and F12, then Ctrl+5, then F5 in Internet Explorer), then submit the form. Look at the form submission data to see what content-type header is being sent, if any.

    It's hard to say much beyond that given that you haven't shown any code, given any examples of the type of data being submitted, or indicated which browser and browser version is being used to submit the form.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #11
    New Coder
    Join Date
    Feb 2014
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks very much for the reply.

    My code snippet is following:
    <html>
    <head>
    <meta http-equiv="Content-Type'" content="text/html; charset=utf-8"/>
    ....
    var xml = "<form id='upload' name='upload' action='insertDB.php' onsubmit='postdata()' method='post' enctype='multipart/form-data'>" +
    "<fieldset>" + "<legend>Add Record</legend>" + "<label for='name'>Name:</label>" + "<input type='text' id='name' name='name'/><br/>" + "<label for='file'>file:</label>" + "<input type='file' id='uploadfile' name='uploadfile' value='uploadfile'/>" + "<button type='submit' id='submit' name='submit' value='submit'>Add</button>" + "</fieldset>" + "</form>";

    function postdata() {
    var formElements = document.getElementById("upload");
    var url = formElements.action;
    postFormData(url, formElements, function(message, responseCode) {
    ....
    }
    };

    function postFormData(url, data, callback) {
    if (typeof FormData == "undefined")
    throw New Error("FormData is not implemented");
    var request = new XMLHttpRequest;
    request.open("POST", url, true);
    request.setRequestHeader("Accept-Encoding", "UTF-8");
    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
    request.send(new FormData(data));
    }
    I use Firefox 16.0.2. Firefox for openSUSE openSUSE-11.4.
    I checked the brower's network inspector. The result is:
    Response Headers pretty print

    HTTP/1.1 200 OK
    Date: Mon, 12 May 2014 03:02:00 GMT
    Server: Apache/2.2.17 (Linux/SUSE)
    X-Powered-By: PHP/5.3.5
    Content-Length: 418
    Keep-Alive: timeout=15, max=100
    Connection: Keep-Alive
    Content-Type: text/plain, charset=utf-8

    Request Headers view source
    Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Encoding gzip, deflate
    Accept-Language en-US,en;q=0.5
    Connection keep-alive
    Host localhost
    Referer http://localhost/phpsqlinfo_add.html
    User-Agent Mozilla/5.0 (X11; Linux i686; rv:16.0) Gecko/20100101 Firefox/16.0 FirePHP/0.7.4
    x-insight activate

    Request Headers From Upload Stream
    Content-Length 292644
    Content-Type multipart/form-data; boundary=---------------------------122837670421124298531877876257
    Please let me know if there is any errors in my code or if you have any idea. Thanks in advance.
    Last edited by jianmarf; 05-12-2014 at 04:28 AM.

  • #12
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,300
    Thanks
    28
    Thanked 275 Times in 269 Posts
    Quote Originally Posted by jianmarf View Post
    I use Firefox 16.0.2. Firefox for openSUSE openSUSE-11.4.
    The current version of Firefox for Linux is 29.0.1: https://www.mozilla.org/en-US/firefox/all/.

    Quote Originally Posted by jianmarf View Post
    I checked the brower's network inspector. The result is:
    Based on your response headers, you're sending something with the text/plain media type, as I suspected. Make sure that whatever file or data that response header is associated with should, indeed, have the text/plain MIME type.

    I think the heart of the issue though is that the media type's charset parameter is separated from the media type with a comma rather than a semicolon. (content-type: text/plain, charset=utf-8 is an invalid header whereas content-type: text/plain; charset=utf-8 is valid.)
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #13
    New Coder
    Join Date
    Feb 2014
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks very much for the reply.

    1.
    Make sure that whatever file or data that response header is associated with should, indeed, have the text/plain MIME type.
    The file uploaded is a .jpg file.
    2.
    I think the heart of the issue though is that the media type's charset parameter is separated from the media type with a comma rather than a semicolon. (content-type: text/plain, charset=utf-8 is an invalid header whereas content-type: text/plain; charset=utf-8 is valid.)
    How does it happen and how can I correct it? I use
    <meta http-equiv="Content-Type'" content="text/html; charset=utf-8"/>
    instead of
    <meta http-equiv="Content-Type'" content="text/html, charset=utf-8"/>
    .

  • #14
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,300
    Thanks
    28
    Thanked 275 Times in 269 Posts
    Quote Originally Posted by jianmarf View Post
    1.
    The file uploaded is a .jpg file.
    JPEG files should use the media type image/jpeg. (charset parameters aren't used for such files.)

    Quote Originally Posted by jianmarf View Post
    2.
    How does it happen and how can I correct it? I use instead of .
    I'm not sure why your files are being sent with that MIME type.

    If your PHP file is outputting a JPEG image, use the PHP code header('content-type: image/jpeg'); to output the correct header.

    If you're using Apache, try setting addtype image/jpeg .jpeg .jpg in an .htaccess file. Make sure you've specified every file extension you use to mark JPEG files.

    Note that the <meta http-equiv="content-type"> element doesn't fix bad HTTP headers (on the server); an HTTP header will override anything specified by a meta element; meta element information is only used when HTTP headers aren't specified. Not to mention that meta elements can only be used to specify the encoding of HTML documents, not any other type of file.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #15
    New Coder
    Join Date
    Feb 2014
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks very much for the reply.
    I am thinking whether the international characters make the encoding error. Before I insert some Chinese characters in html and <form>, the code worked well. Now, it gets errors even I deleted all the Chinese characters.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •