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.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2014
    Posts
    23
    Thanks
    3
    Thanked 0 Times in 0 Posts

    formdata and the character encoding problems

    Hi,

    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);
    }
    request.send(formdata);
    }

    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
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 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
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 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.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%


  •  

    Posting Permissions

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