Ajax Question

Discussion in 'JavaScript' started by NoamBarz, Jan 5, 2009.

  1. #1
    I need to load some information from a mysql database using ajax. The db table charset and collation are set to utf8.

    here's my ajax function:
    
    //-------------------------------------------------------------------------------------------------------
    function DisplayContent(httpRequest, element) { 
            var elem = document.getElementById(element);
            if (httpRequest.readyState == 4) {
                if (httpRequest.status == 200) {
    	             elem.innerHTML = httpRequest.responseText;
    	             return;
                } else {
                    alert('There was a problem with the request.');
                }
            }
    }
    //-------------------------------------------------------------------------------------------------------
    
    var httpRequest;
    
    //-------------------------------------------------------------------------function makeRequest(url, element) {
            	
            //var httpRequest;
    
            if (window.XMLHttpRequest) { // Mozilla, Safari, ...
                httpRequest = new XMLHttpRequest();
                if (httpRequest.overrideMimeType) {
                    httpRequest.overrideMimeType('text/xml');
                }
            } 
            else if (window.ActiveXObject) { // IE
                try {
                    httpRequest = new ActiveXObject("Msxml2.XMLHTTP.5.0"); //new ActiveXObject("Msxml2.XMLHTTP");
                    } 
                    catch (e) {
                               try {
                                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                               } 
                               catch (e) {}
                   }
            }   
    
            if (!httpRequest) {
                alert('Giving up :( Cannot create an XMLHTTP instance');
                return false;
            }
    
            httpRequest.onreadystatechange = function() { DisplayContent(httpRequest, element); };
            httpRequest.open('GET', url, true);
            httpRequest.send(null);
    
    }
    //-------------------------------------------------------------------------
    
    Code (markup):

    so using javascript, in order to dump db info into an HTML elementy, all Ineed to so is supply the url of the page that will select the data and the element in which it should display the data.

    My problem has to do with the encoding. my php file - the one that selects the data is saved in utf-8 format. if I assign a varraible a value in hebrew, it will display just fine on the HTML page. When I select a record from my table however, it doesn't dislpay properly - I get jibberish.

    I use $result=mysql_query("SET NAMES 'utf8'"); but that doesn't help either.

    does anyone know what's going on? writing actual text in the code should guarantee that it is formatted in utf-8. Since data from the db won't display, does that mean it is somehow not in utf-8?
     
    NoamBarz, Jan 5, 2009 IP
  2. rags02

    rags02 Member

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #2
    in your php file, if you just return something like:
    echo '<pre>';
    print_r( $_GET );
    echo '</pre>';
    PHP:
    ... does it output correctly on your HTML page?
     
    rags02, Jan 6, 2009 IP
  3. yoavmatchulsky

    yoavmatchulsky Member

    Messages:
    57
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #3
    maybe your PHP file outputs the file in non-UTF8 encoding.
    try adding this to your server-side code:

    
    header('Content-Type: text/html; charset=UTF-8');
    
    PHP:
     
    yoavmatchulsky, Jan 7, 2009 IP