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

    formdata and the character encoding problems


    I used FormData to post the data of a form to a php file. The form in the html is:

    <form id='upload' action='php_add.php' onsubmit='postdata()' method='POST' enctype='multipart/form-data'>
    <input type='text' id='name'>

    function postdata() {
    var formElements = document.getElementByID("upload");
    var url = formElement.action;
    var request = new XMLHttpRequest();
    request.open("POST", url);
    var formdata = new FormData();
    for (var name in data) {
    if (!data.hasOwnProperty(name)) continue;
    var value = data[name];
    if (typeof(value) === "function") continue;
    formdata.append(name, value);

    When I push the "submit" button in the html, I got two error messages.

    1. NS_ERROR_CANNOT_CONVERT_DAT: (nsIDOMFormData.append).
    Do I need to process inputs, like escape('input text') to get the correct input format?

    2. php_add.php
    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 be declared in the document or in the transfer protocol.
    For this error message, I already add <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="encoding" content="utf-8">
    Also, I tried add <meta...> on the php_add.php file, but the error is still there.

    Any suggestion is very welcome! Thanks!

  2. #2
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Behind the Wall
    Thanked 567 Times in 560 Posts
    1. check what data types you have in each iteration. FormData.append() only allows File, Blob and String data as value.

    2. where does that error come from?
    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. #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Thanked 626 Times in 605 Posts
    you might need to specify async on open() to send formData. i would also try formdata.append(name, String(value) ); and see if that helps. It shouldn't matter, but it looks like some browsers had/have trouble coercing some input into strings. Finally, you should cancel the actual form submit as you're intercepting it with ajax. If none of that works, post the whole code you're using, not just a snip.
    Last edited by rnd me; 02-12-2014 at 10:25 PM.
    Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com


Posting Permissions

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