61

I have a page that contains a form. This page is served with content type text/html;charset=utf-8. I need to submit this form to server using ISO-8859-1 character encoding. Is this possible with Internet Explorer?

Setting accept-charset attribute to form element, like this, works for Firefox, Opera etc. but not for IE.

<form accept-charset="ISO-8859-1">
  ...
</form>

Edit: This form is created by server A and will be submitted to server B. I have no control over server B.

If I set server A to serve content with charset ISO-8859-1 everything works, but I am looking a way to make this work without changes to server A's encoding. I have another question about setting the encoding in server A.

Community
  • 1
  • 1
Juha Syrjälä
  • 33,425
  • 31
  • 131
  • 183

11 Answers11

35

There is a simple hack to this:

Insert a hidden input field in the form with an entity which only occur in the character set the server your posting (or doing a GET) to accepts.

Example: If the form is located on a server serving ISO-8859-1 and the form will post to a server expecting UTF-8 insert something like this in the form:

<input name="iehack" type="hidden" value="&#9760;" />

IE will then "detect" that the form contains a UTF-8 character and use UTF-8 when you POST or GET. Strange, but it does work.

Collin Anderson
  • 14,787
  • 6
  • 68
  • 57
  • 7
    This does not seem to work for the inverse situation. Posting a form which stays on a utf-8 page to an iso-8859-1 page on an other server. – MaxiWheat Jan 11 '10 at 16:59
  • 1
    Right, it won't work the other way. Wenn you specify something like "ÿ" which is the "highest" Char in the ISO-8859-1 charset, this obviously is an UTF-8 code, too. So, IE therefore thinks it's UTF-8. So it only works when the target charset has a greater amount of characters. – acme Oct 19 '10 at 08:41
  • Doesn't work for me in IE8, please see [the testcase](http://artax.karlin.mff.cuni.cz/~ttel5535/pub/bugs/form_accept-charset/non_utf8_uri_test-fix-v1.html) – Tomas Oct 31 '13 at 08:41
  • this does not not work on UTF-8 pages for a more universal workaround in IE see the answer of dgaspar below. – aPokeIntheEye Mar 01 '17 at 10:56
34

With decent browsers:

<form accept-charset="ISO-8859-1" .... >

With IE (any):

document.charset = 'ISO-8859-1'; // do this before submitting your non-utf8 <form>!
dgaspar
  • 655
  • 6
  • 10
  • 5
    This seems to solve the IE problem (when JavaScript is enabled in the browser), and you can implement it by using the attribute `onsubmit="document.charset = 'ISO-8859-1'"` in the `form` tag. – Jukka K. Korpela Aug 30 '12 at 12:15
  • The `Accept-Charset` header is used to tell the server which charset is acceptable as response (see http://tools.ietf.org/html/rfc2616#section-14.2). It seems to me that this is not the correct way to specify the charset of the submitted document. Per standard, this should be added as a `charset` parameter to the content-type. Not sure how to specify this in HTML forms though :\ – exhuma Nov 04 '14 at 07:53
  • After a bit more digging, I came across the [enctype](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#attr-enctype) attribute on the form element. Following the description, this looks more correct. Have not tested it yet though. – exhuma Nov 04 '14 at 07:56
  • the onsubmit = "document.chartset = 'ISO-8859-1'" did the trick. Just tested it on IE 11.Thanks. – Manish Pradhan Oct 05 '15 at 14:06
  • @exhuma that section is about "accept-charset" on header. OP is declaring accept-charset on Form markup. – Carlos Henrique Cano Dec 09 '16 at 12:16
6

It seems that this can't be done, not at least with current versions of IE (6 and 7).

IE supports form attribute accept-charset, but only if its value is 'utf-8'.

The solution is to modify server A to produce encoding 'ISO-8859-1' for page that contains the form.

Juha Syrjälä
  • 33,425
  • 31
  • 131
  • 183
  • 2
    IE only uses `accept-charset` as a ‘backup’, when it has failed to encode a fields in the page's own charset. Specify `accept-charset` and you won't be able to tell which encoding was used. It is, therefore, utterly useless; it should never be used. The only solution is, indeed, to change the charset of the page (or iframe) containing the form. – bobince Jan 07 '10 at 02:33
5

I've got the same problem here. I have an UTF-8 Page an need to post to an ISO-8859-1 server.

Looks like IE can't handle ISO-8859-1. But it can handle ISO-8859-15.

<form accept-charset="ISO-8859-15">
  ...
</form>

So this worked for me, since ISO-8859-1 and ISO-8859-15 are almost the same.

Christian
  • 67
  • 1
  • 1
2

If you have any access to the server at all, convert its processing to UTF-8. The art of submitting non-UTF-8 forms is a long and sorry story; this document about forms and i18n may be of interest. I understand you do not seem to care about international support; you can always convert the UTF-8 data to html entities to make sure it stays Latin-1.

Edward Z. Yang
  • 26,325
  • 16
  • 80
  • 110
  • 1
    I do care a lot about international support, and I'd like to keep my app in UTF-8 world, but this single page needs to be submitted in iso-8859-1 to external server. – Juha Syrjälä Nov 21 '08 at 15:32
1

For Russian symbols 'windows-1251'

<form action="yourProcessPage.php" method="POST" accept-charset="utf-8">
<input name="string" value="string" />
...
</form>

When simply convert string to cp1251

$string = $_POST['string'];
$string = mb_convert_encoding($string, "CP1251", "UTF-8");
dr.dimitru
  • 2,645
  • 1
  • 27
  • 36
  • 1
    Although in itself this does not answer the question per se, I think this is a much better solution. You should anyway always work with UTF-8. This being said, if you create a form that is sent to a server over which you do not have control, it won't work this way. – Alexis Wilke Dec 26 '13 at 01:51
1

Just got the same problem and I have a relatively simple solution that does not require any change in the page character encoding(wich is a pain in the ass).

For example, your site is in utf-8 and you want to post a form to a site in iso-8859-1. Just change the action of the post to a page on your site that will convert the posted values from utf-8 to iso-8859-1.

this could be done easily in php with something like this:

<?php
$params = array();
foreach($_POST as $key=>$value) {
    $params[] = $key."=".rawurlencode(utf8_decode($value));
}
$params = implode("&",$params);

//then you redirect to the final page in iso-8859-1
?>
  • 1
    It's a good idea, but this can be quite tricky if you are trying to submit login information and need to handle cookies and stuff. This could be done with CURL then, but it's really a pain to get it working. – acme Nov 05 '10 at 14:23
0

Looks like Microsoft knows accept-charset, but their doc doesn't tell for which version it starts to work...
You don't tell either in which versions of browser you tested it.

PhiLho
  • 40,535
  • 6
  • 96
  • 134
0

I seem to remember that Internet Explorer gets confused if the accept-charset encoding doesn't match the encoding specified in the content-type header. In your example, you claim the document is sent as UTF-8, but want form submits in ISO-8859-1. Try matching those and see if that solves your problem.

warp
  • 1,508
  • 15
  • 21
  • I have a similar problem. The application serves the response as utf-8 and in the form it is specified accept-charset="utf-8" but with IE8 it retrieves the values with wrong format. What is the best configuration to work with forms with Internet Explorer? Thanks. – David García González Jun 22 '09 at 12:30
-1

I am pretty sure it won't be possible with older versions of IE. Before the accept-charset attribute was devised, there was no way for form elements to specify which character encoding they accepted, and the best that browsers could do is assume the encoding of the page the form is in will do.

It is a bit sad that you need to know which encoding was used -- nowadays we would expect our web frameworks to take care of such details invisibly and expose the text data to the application as Unicode strings, already decoded...

pdc
  • 2,314
  • 20
  • 28
-3
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
sth
  • 222,467
  • 53
  • 283
  • 367