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
- <html><head><title></title><meta http-equiv="Content-type" content="text/html; charset=Windows-1252"/>
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
- <script>
- $(document).ready(function()
- {
- $("#myButton").click(function() {getTownName() });
- });
- function getTownName()
- {
- var cook = readCookie("TEST1") ;
- if (cook!=null)
- {
- document.cookie="TEST2="+cook; // create new cookie
- startAjax("","#display")
- }
- }
- function startAjax(params,id)
- {
- var request = $.ajax(
- {
- url: "test2.php",
- type: "POST",
- data: params,
- dataType: "html"
- });
-
- request.done(function( msg )
- {
- $( id ).html( msg );
- });
- request.fail(function( jqXHR, textStatus ) {
- alert( "Request failed: " + textStatus );
- });
- }
- function readCookie(name)
- {
- var nameEQ = name + "=";
- var ca = document.cookie.split(';');
- for(var i=0;i < ca.length;i++) {
- var c = ca[i];
- while (c.charAt(0)==' ') c = c.substring(1,c.length);
- if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
- }
- return null;
- }
- </script>
- </head><body><center>
- <?php
- setcookie("TEST1","Cazères");
- if (! isset($_COOKIE["TEST1"])) die("please refresh this page to create my sample cookie.");
- $town = $_COOKIE["TEST1"];
- echo "Town name before clicking : ".$town;
- ?>
- <p id="display"></p>
- <button id="myButton">Click here
- </center></body></html>
Test2.php
- <?php
- echo "Town name after clicking without encoding : ". $_COOKIE["TEST2"]."<p>";
- echo "Town name after clicking with encoding : ". utf8_encode($_COOKIE["TEST2"]);
- ?>