Alberto
Alberto

Reputation: 396

html <head> not working properly

I have the following problem:

I have loaded a page using a basic xmlhttp request like this:

xmlhttp = getXMLHTTP();

xmlhttp.open("POST",link,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");

xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        document.getElementById("principal").innerHTML=xmlhttp.responseText;
    }
}   

xmlhttp.send();

Where link is the source of the file that I want to call dynamically and principal is the div where I want it to be loaded. This does not seem to be the main error of my problem (which I will explain below) since I have tested it with many pages.

My problem now remains on the fact that I want to have a "popup" box which is in the page loaded with xmlhttp that will show the terms and conditions of my webpage, and the head html tag is not being parsed since the charset UTF-8 is not respected: the accents and special characters appear with a ? symbol instead of the right one.

I load this popup with the following PHP code:

<?php include_once('../../info/terms_and_conds.html'); ?>

And the HTML code which is inside this file is:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title>Terms and Conditions</title>
</head>
<body>

This is a test á  é  í  ó  ú   ñ   ç 

</body>
</html>

Finally, the output is the following one:

This is a test � � � � � � �

Note: I would have wanted to post a picture of it, but I don't have enough reputation. Sorry for the inconveniences

I also have to tell that all accents and special characters are displayed properly in the page loaded with the xmlhttp Request. Could it be the include_once() function the one that is producing this problem?

Thanks in advance.

Upvotes: 1

Views: 2166

Answers (3)

Jon..
Jon..

Reputation: 440

This is really just due to encoding - If you change that meta-tag that has "UTF-8" in it to the following:

<meta http-equiv="Content-type" content="text/html; charset= ISO-8859-1" />

Then everything should appear as it is supposed to.

Upvotes: 1

Funk Forty Niner
Funk Forty Niner

Reputation: 74219

The problem here is that your files are not saved as/encoded as UTF-8.

As stated in comments, using a code editor (Notepad++ was suggested) open the file(s) and check its encoding.

If it isn't UTF-8, convert it to that, then save the file(s).

Reference:

Although there are other code editors that will do the same operation.


Footnotes:

If you have any issues later on and getting an headers already sent... warning, then you will need to encode as UTF-8 without BOM.

A BOM (a.k.a. byte order mark) counts as output (before header).

Here is more on the subject:

Upvotes: 3

ptkoz
ptkoz

Reputation: 2497

Probably your webserver is sending different charset headers than you specified in your <meta/> tag. Try to specify content type header explicitly in PHP file.

<?php 
    header('Content-Type: text/html; charset=UTF-8');
    include_once('../../info/terms_and_conds.html'); 
?>

Upvotes: 2

Related Questions