...

View Full Version : Character encoding problem in Ajax request



jarimh1984
07-26-2008, 05:37 PM
Simplified example of my problem code:



function search(){

var search = document.getElementById('searchstring').value;
xmlHttp=GetXmlHttpObject()

if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="search.php"
url=url+"?string="+search
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}


search.php
Code:


<?php header('Content-Type: text/html; charset=utf-8');
$searchstring = mysql_escape_string($_GET['string']);
echo $searchstring;
?>

In firefox everything works fine, but in Internet Explorer if I send a searchvalue like öööööö, it echos this �� . If I put $searchstring = utf8_encode($searchstring); in search.php, then it works in Explorer, but of course not in firefox. What would be a good way to fix this.

scoop_987
07-26-2008, 06:36 PM
you can try this on the search.php



<?PHP
//Drop the header as it is NOT required:
echo urldecode($searchstring);

jarimh1984
07-27-2008, 09:50 AM
Thanks for the suggestion, but it did not help.

jarimh1984
07-27-2008, 04:26 PM
The fix was so simple :)

var search = encodeURIComponent(document.getElementById('searchstring').value);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum