Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts

    sending and retreiving a string messes up the accents

    Hello,

    I'm having trouble with the following "setup":

    ajaxTest.php (this is the file you should open to test)
    Code:
    <!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN" 
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>
        <title>Ajax test</title>
    	<script type="text/javascript">
    	function tryACountry()
    	{
    		trouverVilles("République Tchèque");
    	}
    	function trouverVilles(pays)
    	{
    		var xmlhttp;
    		if (window.XMLHttpRequest)
    		{
    			// code for IE7+, Firefox, Chrome, Opera, Safari
    			xmlhttp = new XMLHttpRequest();
    		}
    		else if (window.ActiveXObject)
    		{
    			// code for IE6, IE5
    			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    		}
    		else
    		{
    			//window.location = "IllDoThisLater.php";
    			return false;
    		}
    		xmlhttp.onreadystatechange = function()
    		{
    			if(xmlhttp.readyState == 4)
    			{
    				if(xmlhttp.responseText != "")
    				{
    					afficherVilles(xmlhttp.responseText);
    				}
    				else
    				{
    					alert(xmlhttp.responseText);
    					//alert("Aucune ville n'a été trouvée pour ce pays, veuillez recharger la page et réessayer OU faire votre recherche d'université par le formulaire sous la carte");
    				}
    			}
    		}
    		alert(pays);
    		xmlhttp.overrideMimeType('text/xml; charset=iso-8859-1');
    		var url = "ajaxTestSupportPage.php?pays=" + pays;
    		xmlhttp.open("GET", url, true);
    		xmlhttp.send(null);
    	}
    	function afficherVilles(villesString)
    	{	
    		alert(villesString);
    		//var villes = villesString.split(" ");
    	}
        </script>
      </head>
      <body onload="tryACountry()">
      </body>
    </html>
    ajaxTestSupportPage.php (You need this file for the ajax to happen)
    Code:
    <?php
    	header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
    	header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
    	header('Content-Type: text/html; charset=iso-8859-1'); 
    	if(isset($_GET['pays']) && $_GET['pays'] != "")
    	{
    		echo($_GET['pays']);
    	}
    ?>

    How to see my problem:

    1. Create ajaxTest.php and ajaxTestSupportPage.php and place them in the same folder
    2. Run ajaxTest.php.


    What happens
    1. ajaxTest.php alerts the string given to the tryACountry() function as an parameter.
    2. It sends the same string over to ajaxTestSupportPage.php.
    3. ajaxTestSupportPage.php echos the string as is.
    4. ajaxTest.php get's this as a response and alerts the response.


    My problem
    The first alert shows the string passed as a parameter to the tryACountry() function (République Tchèque), whereas the second alert shows the same text, but with the accents all messed up (République Tchèque).

    Why do the accents mess up like this?
    Shawn

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    use encodeURI when you are passing mutibyte strings
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Nope, still having problems. Here is the new code:
    ajaxTest.php
    Code:
    <!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN" 
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>
        <title>Ajax test</title>
    	<script type="text/javascript">
    	function tryACountry()
    	{
    		trouverVilles("République Tchèque");
    	}
    	function trouverVilles(pays)
    	{
    		var xmlhttp;
    		if (window.XMLHttpRequest)
    		{
    			// code for IE7+, Firefox, Chrome, Opera, Safari
    			xmlhttp = new XMLHttpRequest();
    		}
    		else if (window.ActiveXObject)
    		{
    			// code for IE6, IE5
    			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    		}
    		else
    		{
    			//window.location = "IllDoThisLater.php";
    			return false;
    		}
    		xmlhttp.onreadystatechange = function()
    		{
    			if(xmlhttp.readyState == 4)
    			{
    				if(xmlhttp.responseText != "")
    				{
    					afficherVilles(xmlhttp.responseText);
    				}
    				else
    				{
    					alert("error: " + xmlhttp.responseText);
    					//alert("Aucune ville n'a été trouvée pour ce pays, veuillez recharger la page et réessayer OU faire votre recherche d'université par le formulaire sous la carte");
    				}
    			}
    		}
    		alert(pays);
    		xmlhttp.overrideMimeType('text/xml; charset=iso-8859-1');
    		var url = "ajaxTestSupportPage.php?pays=" + encodeURI(pays);
    		xmlhttp.open("GET", url, true);
    		xmlhttp.send(null);
    	}
    	function afficherVilles(villesString)
    	{	
    		alert(villesString);
    		//var villes = villesString.split(" ");
    	}
        </script>
      </head>
      <body onload="tryACountry()">
      </body>
    </html>
    ajaxTestSupportPage.php (unchanged)
    Code:
    <?php
    	header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
    	header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
    	header('Content-Type: text/html; charset=iso-8859-1'); 
    	if(isset($_GET['pays']) && $_GET['pays'] != "")
    	{
    		echo($_GET['pays']);
    	}
    ?>
    Last edited by shlagish; 07-20-2009 at 03:47 PM.
    Shawn


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •