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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Oct 2008
    Posts
    214
    Thanks
    5
    Thanked 22 Times in 22 Posts

    AJAX with PHP encoding issue

    My whole website is UTF-8 encoded, all my files (.JS, .HTML, .PHP) are saved in UTF-8 (no BOM).

    I always use
    PHP Code:
    header('Content-Type: text/html; charset=UTF-8'); 
    at the beggining of my PHP files.

    I also use
    Code:
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    for my HTML files.

    But I still have an encoding issue...

    I have an AJAX request that send me data to output in the DOM...

    This is my PHP file that answers to the AJAX requests:
    PHP Code:
    <?php
    header
    ('Content-Type: text/html; charset=UTF-8');

    if (
    array_key_exists('str'$_GET))
        
    $myStr $_GET['str'];
    else
        
    $myStr '';

    echo(
    $myStr);
    ?>
    This is the JavaScript that do the AJAX request:
    Code:
    function TestUTF()
    {
        var request = CreateRequest();
        var unicodeTest = document.getElementById('unicodeTest');
        if (request != null && unicodeTest)
        {
            unicodeTest = unicodeTest.value;
            var ajaxUrl = 'test-ajax.php?str=' + escape(unicodeTest);		
    
            request.open('POST', ajaxUrl, true);
            request.onreadystatechange = function(){TestRS(request)};
            request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            request.send(null);		
        }
    }
    
    function TestRS(request)
    {
        if (request.readyState == 4)
        {
            if (request.status == 200)
            {
                var ajaxResults = document.getElementById('ajaxResults');
                if (ajaxResults)
                {
                    var newP = document.createElement('p');
                    newP.appendChild(document.createTextNode('AJAX write : ' + request.responseText));                
                    ajaxResults.appendChild(newP);
                }
            }
            else
                alert('HTTP status ' + request.status);
        }
    }
    
    TestUTF();
    As you can see this is a really simple project that get UTF-8 data for an HTML <input> element, sends it's value (content) through JavaScript then it's processed by PHP and then written back to the HTML (via the DOM) with JavaScript.

    As far as I can see it's the PHP that screw my UTF-8 string... How can it be? I'm telling the browser that it's UTF-8 with header.

    If I add utf8_encode to the echo all is right, but the purpose of this test is to get rid of these utf8_encode.

    Anyone can help? I can provide full sources of this sample project if you want to try it.

    Thanks!
    Last edited by AlexV; 03-03-2009 at 08:41 PM.

  • #2
    Regular Coder
    Join Date
    Oct 2008
    Posts
    214
    Thanks
    5
    Thanked 22 Times in 22 Posts
    Found my solution!

    Use encodeURIComponent instead of escape in JavaScript!
    Last edited by AlexV; 03-04-2009 at 01:53 PM.

  • #3
    Regular Coder
    Join Date
    Oct 2008
    Posts
    214
    Thanks
    5
    Thanked 22 Times in 22 Posts
    Why does (JavaScript) escape do mess the string encoding when in UTF-8???

  • #4
    Regular Coder
    Join Date
    Oct 2008
    Posts
    214
    Thanks
    5
    Thanked 22 Times in 22 Posts
    I found out that escape only works with ASCII chars. When using Unicode (UTF-8) data with it, the output is corrupted. It even crash IE JavaScript andd XML engine.


  •  

    Posting Permissions

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