Problem with national characters in cookies.

Problem with national characters in cookies.

Hello,

I'm having   French accent problems with  values put in cookies when using JQUERY/AJAX.
Below are my samples.  In fact,  I'm testing a "town name" which contains a French accentued letter.

1.  I create Cookie TEST1  in  test1.php ,  If I read the cookie TEST1  from the same procedure the town name  displays  correctly.

2. Then I click on the button, to trigger a JQUERY function which creates a new cookie : TEST2 from the value found in TEST1.

3. I   start AJAX to call a second   procedure : test2.php  and display the value found in TEST2.

To display correctly the town name, I'm obliged to pass the name to  the  utf_encode function.

Of course if it was only on one word to encode it would not be a problem, but  in my real project, it's much more complicated. 

So my question :

Is it possible to avoid encoding such words in my  php procedures ? (for example with a special jquery or Ajax option) .
 

Many thanks in advance.

Gérard.

test1.php


  1. <html><head><title></title><meta http-equiv="Content-type" content="text/html; charset=Windows-1252"/>
  2. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  3. <script>
  4. $(document).ready(function()
  5. {
  6.      $("#myButton").click(function()  {getTownName() });
  7. });
  8. function getTownName()
  9. {
  10.     var cook    = readCookie("TEST1") ;   
  11.     if (cook!=null)
  12.     {
  13.         document.cookie="TEST2="+cook;   // create new cookie
  14.         startAjax("","#display")
  15.     }   
  16. }
  17. function startAjax(params,id)
  18. {
  19.     var request = $.ajax(
  20.     {
  21.         url: "test2.php",
  22.         type: "POST",
  23.         data: params,
  24.         dataType: "html"
  25.         });
  26.        
  27.         request.done(function( msg )
  28.         {
  29.             $( id ).html( msg );
  30.         });
  31.         request.fail(function( jqXHR, textStatus ) {
  32.         alert( "Request failed: " + textStatus );
  33.     });               
  34. }
  35. function readCookie(name)
  36. {
  37.     var nameEQ = name + "=";
  38.     var ca = document.cookie.split(';');
  39.     for(var i=0;i < ca.length;i++) {
  40.         var c = ca[i];
  41.         while (c.charAt(0)==' ') c = c.substring(1,c.length);
  42.         if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  43.     }
  44.     return null;
  45. }
  46. </script>
  47. </head><body><center>
  48. <?php
  49.      setcookie("TEST1","Cazères");
  50.      if (! isset($_COOKIE["TEST1"]))  die("please refresh this page to create my sample cookie.");  
  51.      $town = $_COOKIE["TEST1"];
  52.      echo "Town name before clicking : ".$town;
  53. ?>
  54. <p id="display"></p>
  55. <button id="myButton">Click here
  56. </center></body></html>

       Test2.php

  1. <?php
  2.      echo "Town name after clicking without encoding : ". $_COOKIE["TEST2"]."<p>";
  3.      echo "Town name after clicking with encoding : ". utf8_encode($_COOKIE["TEST2"]);
  4. ?>