Jump to content

Encoding problem?


Ifaiden

Recommended Posts

I don't know if this is a specific ajax problem or if it's just Internet Explorer. I'm working on an ajax live search function ( i followed the guide here http://www.w3schools.com/php/php_ajax_livesearch.asp). I'm converting an excel-file to XML.

When I fill in the form with the letters å, ä, ö (Swedish) in internet explorer I get no hits. In any other browsers it works fine.

 

I have defined UTF-8 in all the browsers, tested ISO-8859-1 (recommended Swedish) and even tried combining them. Nothing works...

Any ideas?

 

Example:

http://mindu.mine.nu/amazing_solutions/XLStoXML/search.html

 

XML-file:

http://mindu.mine.nu/amazing_solutions/XLStoXML/Varderingar.xml

 

XLS To XML

<?php 
header('Content-Type: text/html; charset=UTF-8');
require_once('/var/www/amazing_solutions/XLStoXML/reader.php');

$data = new Spreadsheet_Excel_Reader();
$data->setUTFEncoder('iconv');
$data->setOutputEncoding('CP-1251'); 

// Read in excel file
$data->read('/var/www/amazing_solutions/XLStoXML/Varderingar.xls');

//Count XLS-data rows

// Get first sheet
$sheet = $data->sheets[0];
// Get all rows in the sheet
$rows = $sheet['cells'];

// Find total number of rows
$rowCount = count($rows);

/*
// Show data from last row
echo "<table><tr>";
foreach($rows[$rowCount -1] as $col) {
echo "<td>$col</td>";
}
echo "</tr></table>";
*/


//Skips the two first rows (Data begins at row 3)
$rowCount=$rowCount-2;

echo "Antalet rader data i excel-filen (Data börjar i rad 3): ".$rowCount."";
  

// End of count XLS-data rows

$myFile = "Varderingar.xml";
$fh = fopen($myFile, 'w') or die("$myFile gick inte att hitta");


$stringData = "<?xml version='1.0' encoding='ISO-8859-1'?>\n<pages>\n"; 
fwrite($fh, $stringData);

//Import XLS-data to XML-file in right format

$loop_count = 0;

//Data begins in row 3
$row_start_count = 3; 

$search  = array('&');
$replace = array('&'); 

//title = Kolumn A, URL = ISIN.htm
while($loop_count!=$rowCount){

$stringData = "<link>\n<namn>".str_replace($search, $replace, $data->sheets[0]['cells'][$row_start_count][1])."</namn>\n<url>".$data->sheets[0]['cells'][$row_start_count][5].".htm</url>\n";
fwrite($fh, $stringData);

$stringData = "<dagskurs>".$data->sheets[0]['cells'][$row_start_count][2]."</dagskurs>\n";
fwrite($fh, $stringData);

$stringData = "<langsiktigtvarde>".$data->sheets[0]['cells'][$row_start_count][3]."</langsiktigtvarde>\n";
fwrite($fh, $stringData);

$stringData = "<index>".$data->sheets[0]['cells'][$row_start_count][4]."</index>\n";
fwrite($fh, $stringData);

$stringData = "<isin>".$data->sheets[0]['cells'][$row_start_count][5]."</isin></link>\n";
fwrite($fh, $stringData);

$loop_count++;
$row_start_count++;

}


// End of import XLS-data to XML-file


$stringData = "</pages>";
fwrite($fh, $stringData);



fclose($fh);


?>

 

Livesearch.php

<?php
header('Content-Type: text/html; charset=UTF-8');
$xmlDoc=new DOMDocument();
$xmlDoc->load("Varderingar.xml");


$x=$xmlDoc->getElementsByTagName('link');


//get the q parameter from URL
$q=$_GET["q"];

//lookup all links from the xml file if length of q>0
if (strlen($q)>0)
{
$hint="";
for($i=0; $i<($x->length); $i++)
  {
  $y=$x->item($i)->getElementsByTagName('namn');
  $z=$x->item($i)->getElementsByTagName('url');
  $t=$x->item($i)->getElementsByTagName('isin');
  $a=$x->item($i)->getElementsByTagName('dagskurs');
  $b=$x->item($i)->getElementsByTagName('langsiktigtvarde');
  $c=$x->item($i)->getElementsByTagName('index');


  if ($y->item(0)->nodeType==1 && $t->item(0)->nodeType==1)
    {
    //find a link matching the search text
    if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q) || stristr($t->item(0)->childNodes->item(0)->nodeValue,$q) )
      {
      if ($hint=="")
        {
        $hint=
	"<table border='0' width='850' cellspacing='0' cellpadding='5px'>
		<tr id='headertable'>
			<td width='350px'>
				<b><font size='2'>Produkt </font></b>
			</td>

			<td width='95px'>
				<b><font size='2'>Isinkod </font></b>
			</td>

			<td width='105px'>
				<b><font size='2'>Indiaktivt Marknadsvärde </font></b>
			</td>

			<td width='78px'>
				<b><font size='2'>Långsiktigt Värde </font></b>
			</td>

			<td width='90px'>
				<b><font size='2'>Underliggande Index </font></b>
			</td>
			</tr>
	</table><table border='0' width='850' cellspacing='0' cellpadding='5px'><tr class='tablestyle'>
	<td width='350px'>
	<a href='" . $z->item(0)->childNodes->item(0)->nodeValue . "' target='_blank'>" .
        $y->item(0)->childNodes->item(0)->nodeValue .
	"</a><td width='95px'>" .
	$t->item(0)->childNodes->item(0)->nodeValue .
	"</td><td width='105px'>" .
	$a->item(0)->childNodes->item(0)->nodeValue .
	"</td><td width='78px'>" .
	$b->item(0)->childNodes->item(0)->nodeValue .
	"</td><td width='90px'>" .
	$c->item(0)->childNodes->item(0)->nodeValue .
	"</td></tr>
	</table>";

        }


      else
        {
		$hint=$hint . 
		"<table border='0' width='850' cellspacing='0' cellpadding='5px'><tr class='tablestyle'><td width='350px'><a href='" . $z->item(0)->childNodes->item(0)->nodeValue . "' target='_blank'>" .
		$y->item(0)->childNodes->item(0)->nodeValue . 
		"</a><td width='95px'>" .
		$t->item(0)->childNodes->item(0)->nodeValue . 
		"</td><td width='105px'>" .
		$a->item(0)->childNodes->item(0)->nodeValue .
		"</td><td width='78px'>" .
		$b->item(0)->childNodes->item(0)->nodeValue .
		"</td><td width='90px'>" .
		$c->item(0)->childNodes->item(0)->nodeValue .
		"</td> 
		</tr>
		</table>";
        }
      }
    }
  }
}

// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint=="")
  {
  $response="Inga träffar";
  }
else
  {
  $response=$hint;
  }

//output the response
echo $response;
?> 

 

search.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head> 

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>LiveSearchTest</title>
<style type="text/css">
#livesearch{
width: 850px; 
padding-left: 10px;
padding-top: 5px;

}

#livesearch a:link, #livesearch a:visited, #livesearch a:focus{
text-decoration:  none;
outline: none;
color: #808080;
    font-family: arial;
    font-size: 12px;
}

#livesearch a:hover{
text-decoration:  underline;
color: #505050;
}

#header {
text-decoration:  none;
outline: none;
color: #808080;
    font-family: arial;
    font-size: 12px;
}

.tablestyle {
text-decoration:  none;
outline: none;
color: #808080;
    font-family: arial;
    font-size: 12px;
text-align: left;
}

#headertable {
    color: #808080;
    font-family: arial;
    font-size: 11px;
}
</style>

<script type="text/javascript">
function showResult(str) 
{
if (str.length==0)
  {
  document.getElementById("livesearch").innerHTML="";
  document.getElementById("livesearch").style.border="0px";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
    document.getElementById("livesearch").style.border="1px solid #A5ACB2";
    }
  }
xmlhttp.open("GET","livesearch.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>


<body>

<form action="">
<input type="text" size="30" onkeyup="showResult(this.value)" value='Sök här...' onfocus="if(this.value==this.defaultValue){this.value='';}" onblur="if(this.value==''){this.value=this.defaultValue;}" />
<div id="livesearch"></div>
</form>

</body>
</html> 

Link to comment
https://forums.phpfreaks.com/topic/231893-encoding-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.