views:

394

answers:

4

I'm trying to take the values from a <textarea> and pass it via XMLHttpRequest to a PHP page that adds the content to a database.

However, when it reaches the database, the "å æ ø" characters are converted to "Ã¥ æ Ã".

I've searched high and low and tried to change to UTF-8, tried to use JavaScript versions of htmlentities()/htmlspacialchars() etc, but no matter what I try to do, the result is always the same.

My page is set to iso-8859-15 and the same is the requestType for the request.

Because I haven't had to use Ajax too much in my work, I've snipped the setup procedure from the net. It looks like this:

var page_request = false;
var contentType = "application/x-www-form-urlencoded;charset=iso-8859-15";  

if (window.XMLHttpRequest)
{
  page_request = new XMLHttpRequest();
}

// If the Browser is Internet Explorer
else if (window.ActiveXObject)
{
  try
  {
    page_request = new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch (e)
  {
    try
    {
      page_request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e){}
  }
}
else 
{ 
  return false; 
}

page_request.open('POST', url, true);
page_request.setRequestHeader("Content-Type", contentType);
page_request.send(query);

However, in the PHP file on the receiving end and in the database the special characters are converted.

Any help is much appreciated! I've spent the better part of this day trying to fix this one error...

A: 

First of all, this website supports syntax highlighting, but only if you select the code and click the little 101|010 symbol.

Second, I would recommend ISO-8859-1, but I'm not saying that's guaranteed to work...

WebDevHobo
A: 

what is the datatype of the table column you are writing to?

Rick J
comments belongs to the comments
SilentGhost
A: 

you need to use encodeURIComponent on your textarea content. and use UTF-8 as well.

SilentGhost
+1  A: 

Your charset delcaration is lying. Saying that content is in ISO doesn't magically make it ISO.

Your query variable appears to be in UTF-8 encoding. That is to be expected, because Javascript strings are always Unicode and functions like encodeURIComponent() (which are needed to properly format GET and POST requests) use UTF-8.

IMHO the best thing is to stop using ISO encodings, completely, forever.

If you can't do that yet, you need to serialize data in Javascript yourself or convert UTF-8 to ISO when receiving posted data on the server.

porneL