Page 1 of 1

greek characters in js <script>

Posted: Mon Aug 18, 2008 5:22 am
by gurjit
Hi

I have greek characters stored in latin_swedish_ci collation in a database, which are stored as html special characters

for example athens is stored as A&#952;&#942;&#957;&#945;

when i try and echo the special characters they display as 'A&#952;&#942;&#957;&#945;' and not the greek for athens. This only happens if i echo in <script> tags, for example

Code: Select all

 
<script>
var a = <?php echo $name; ?>
</script>
 


the output of var a will be A&#952;&#942;&#957;&#945;

How can I decode the characters

I am using php version 4.3.10

I tried iconv but it did not work. this is my setup in php.ini

iconv support - enabled
iconv implementation - glibc
iconv library version - 2.3.4
iconv.input_encoding - local and master value is ISO-8859-1
iconv.internal_encoding - local and master value is ISO-8859-1
iconv.output_encoding - local and master value is ISO-8859-1

Re: greek characters in js <script>

Posted: Mon Aug 18, 2008 8:42 am
by ghurtado
HTML special encodings are only for... well, for displaying in HTML, so you will only see them "decoded" in the final page as rendered by the browser, but possibly not in the "view source" screen when you look at your javascript (I assume that is javascript which you posted, there is no "language" attribute in your tag)

Re: greek characters in js <script>

Posted: Tue Aug 19, 2008 3:28 am
by gurjit
This is the full code of what i'm trying to do

works well with english and german languages

i've only included greece in this example.

The idea is to select a country and the territory displays without refresh. The value stored in myLabels[26] = new Array("Territory","A&#952;&#942;&#957;&#945;") - should be 'Athens' in greece language. This needs to display correctly when echo $name is called.

Code: Select all

 
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<script language="javascript" type="text/javascript"> 
 
var myValues = new Array();
var myLabels = new Array();
 
myValues[0] = new Array("0")
myLabels[0] = new Array("Territory")
myValues[22] = new Array("","410")
 
 
myValues[26] = new Array("","422")
 
myLabels[26] = new Array("Territory","A&#952;&#942;&#957;&#945;")
 
 
var currFac = 2;
var currInst = 1;
var currAbt = 0;
 
function populate(){ 
var fld=window.document.forms[0].header_lid; 
var st = window.document.forms[0].header_cid.value;
 
 
if (myValues[st] != undefined) {
clearSelect(fld);
    for(i = 0; i < myValues[st].length; i++){
        fld.options[i] = new Option(myLabels[st][i],myValues[st][i]);
        
        if (myValues[st][i] == currInst) {
            window.document.forms[0].header_lid.options[i].selected = true;
        } else {
            window.document.forms[0].header_lid.options[0].selected = true;
        }
        
    }
}
 
 
}
 
 
 
 
function clearSelect(fld){ 
    if (fld != undefined) {
        for(i = fld.options.length -1; i >= 0; i--){ 
        fld.options[i] = null; 
        }
    }
}
 
</script>
<table border="0" cellspacing="0" cellpadding="0">
  <form action="../html/home.php" method="post" name="FormName">
  <tr> 
    <td><select name="header_cid" class="NavDD" onChange="populate()">
                <option value="0">Country</option>
        
                
    
                <option value="26" selected="selected">Greece</option> 
        
              </select>
      
      
      </td>
    <td width="4">&nbsp;</td>
    <td>
    
 
    <select name="header_lid" class="NavDD" id="header_lid">
    
     
        
        </select></td>
    <td width="20" align="center"><input type="Image" src="../assets/button.gif" width="16" height="16"></td>
  </tr>
  
  
 
  
  </form>
</table>
 
</body>
</html>
 
 
 

Re: greek characters in js <script>

Posted: Tue Aug 19, 2008 9:36 am
by ghurtado
I dont understand why that happens well enough to explain it, but after playing with it a bit, I did figure out how to fix it. Add the following line after you create your option element:

Code: Select all

fld.options[i].innerHTML = myLabels[st][i];
Keep in mind that there is probably a much better way to do this, I seem to remember innerHTML is being deprecated or never was a standard in the first place. The principle remains the same either way: the "value" of the option element is not exactly the same thing as the HTML contained within.

Re: greek characters in js <script> [SOLVED]

Posted: Wed Aug 20, 2008 5:59 am
by gurjit
THANKS ghurtado

THAT WORKED