Saturday, 1 March 2008

Your page has one encoding but your form does not like it?

Forms are funny creatures. They inherit their encoding from the encoding declared in our web page.

Sometimes however, we are not happy with our web page's inherent encoding as the results that come up when our form is posted fail short of our expectations.

Let's take a specific scenario. You have a web page with UTF-8 encoding and a search form directing to a web site with Windows-1253 encoding. What happens when you post the query αγάπη in Greek?

You get something like this:
αγάπη

You send love, and you get gibberish. Now, this is not fair, is it?

But apparently, there is a solution to that. You can add a little parameter to the form, instructing it to send the query in a different encoding. So for example my form was like this:

<form action="http://www.blab.com" method="get" target="_blank">

and after it was like this:

<form action="http://www.blah.com" method="get" enctype="multipart/form-data" accept-charset="Windows-1253" target="_blank">

In other words, you have to add the character set of your desire (matching the destination page encoding) by adding this little bit of code

enctype="multipart/form-data" accept-charset="Windows-1253"


Lets see the two forms (search for the word αγάπη):

The one without the unicode instruction:

















The one with the unicode instruction:













No comments: