XMLHTTP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nemlah

    XMLHTTP

    Hello,
    I am working on a site which utilizes PHP/JS and the new found XMLHTTP
    frenzy to update dynamically.
    WHile the whole mechanism works beautifull, i can't get to display
    greek Characters correct (They show up as ?).
    I tried following solutions to set the XML:lang or simple lang
    parameter in the div to el, but that didn't help.. Greek text in the
    surrounding page is showing up correct..
    Here is the code i am using:
    The JS File
    if (!xmlhttp && typeof XMLHttpRequest! ='undefined') {
    xmlhttp = new XMLHttpRequest( );
    }

    function loadFragmentInT oElement(fragme nt_url, element_id) {
    var element = document.getEle mentById(elemen t_id);
    element.innerHT ML = 'Loading ...';
    xmlhttp.open("G ET", fragment_url);
    xmlhttp.onready statechange = function() {
    if (xmlhttp.readyS tate == 4 && xmlhttp.status == 200) {
    element.innerHT ML = xmlhttp.respons eText;
    }
    }
    xmlhttp.send(nu ll);
    }

    The client PHP:
    <?php
    include ("../_mssqlconnect.p hp");
    include ("get_login_dat a.php");
    ?>
    <script>
    function offerDetails(id )
    {
    loadFragmentInT oElement('sc_og details.php?ogi d='+id, 'ogDetails');
    }
    </script>

    //......

    ?>
    <span lang="el" id="ogDetails" align="left"></span>

    The server PHP:
    <?php
    include ("../_mssqlconnect.p hp");

    //.......

    echo "<div lang=\"el\">
    <table class='sp_table ' width=300 align=center>
    <tr>
    <td align=center><b >{here the greek Text which doesnt come
    out right}<b></td>
    </tr>
    </table></div>";

    ANy hints?
    thanks

  • Martin Honnen

    #2
    Re: XMLHTTP



    Nemlah wrote:

    [color=blue]
    > I am working on a site which utilizes PHP/JS and the new found XMLHTTP
    > frenzy to update dynamically.
    > WHile the whole mechanism works beautifull, i can't get to display
    > greek Characters correct (They show up as ?).[/color]
    [color=blue]
    > element.innerHT ML = xmlhttp.respons eText;[/color]

    responseText is unreliable when it comes to encodings other than UTF-8
    or UTF-16. So if you want to use responseText then make sure your PHP
    page sends UTF-8 encoded text. Or do not use responseText but exchange
    XML and use responseXML, then if your PHP page sends XML with the proper
    XML declaration the XML parser should recognize the encoding and
    properly decode characters. But XML parsers are only required to support
    the encodings UTF-8 and UTF-16 thus if you want to make sure your page
    works with as many browsers as possible then you should use one of those
    encodings for your XML sent.

    --

    Martin Honnen

    Comment

    • Vasilis Dimos

      #3
      Re: XMLHTTP

      Hey thanks for the fast reply,
      I read about the UTF-8 encoding of the strings sent via responseText,
      and i hoped i could setRequestHeade r to the correct charset.. Doesn't
      seem to work though..(unknow n exception error)
      I am trying the XML approach you suggested. Actually i am writing
      manually an XML string and echoing it. All i get so far though is a
      [object] in IE and nothing in firefox.
      Apparently the responseXML returns an Document object. How can i print
      it? I am not really interested right now in XML magic, but if wrapping
      the data in XML helps i am willing to do that..
      Any further hints..?
      Thanks

      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Börni

        #4
        Re: XMLHTTP

        Vasilis Dimos wrote:[color=blue]
        > Hey thanks for the fast reply,
        > I read about the UTF-8 encoding of the strings sent via responseText,
        > and i hoped i could setRequestHeade r to the correct charset.. Doesn't
        > seem to work though..(unknow n exception error)
        > I am trying the XML approach you suggested. Actually i am writing
        > manually an XML string and echoing it. All i get so far though is a
        > [object] in IE and nothing in firefox.
        > Apparently the responseXML returns an Document object. How can i print
        > it? I am not really interested right now in XML magic, but if wrapping
        > the data in XML helps i am willing to do that..
        > Any further hints..?
        > Thanks[/color]

        I had a similar problem not long ago.
        i past again some php code which should help you.

        <?php
        $xmlString = '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n";
        $xmlString .=
        '<root><text xml:lang="de">U mlaute: ä, ö, ü</text></root>';
        header('Content-Type: application/xml');
        echo utf8_encode($xm lString);
        ?>

        As you can see the xml string is build also manually, but is utf8
        encoded before echoed.

        In the Javascript again, you can access the responseXML object with the
        normal W3C DOM Methods.

        Have a look a this page for a good intro:

        Comment

        • Martin Honnen

          #5
          Re: XMLHTTP



          Vasilis Dimos wrote:

          [color=blue]
          > I read about the UTF-8 encoding of the strings sent via responseText,
          > and i hoped i could setRequestHeade r to the correct charset.. Doesn't
          > seem to work though..(unknow n exception error)[/color]

          The PHP script needs to send UTF-8 encoded text so I am not sure what
          you are trying to achieve with setRequestHeade r on the client.




          --

          Martin Honnen

          Comment

          • Martin Honnen

            #6
            Re: XMLHTTP



            Vasilis Dimos wrote:

            [color=blue]
            > I am trying the XML approach you suggested. Actually i am writing
            > manually an XML string and echoing it. All i get so far though is a
            > [object] in IE and nothing in firefox.[/color]

            I do not speak Greek so I am relying on babelfish for this. It
            translates English
            Who is god?
            to the following:
            Ποιος είναι Θεός;

            Now assuming you are encoding your PHP pages as ISO-8859-7 which is an
            8-bit encoding allowing you to use Latin letters and Greek letters you
            could send your XML as ISO-8859-7 and then rely on the XML parser to
            decode that. As said XML parsers do not need to understand that encoding
            but my tests here with MSXML/IE 6, Mozilla and with Opera 8.00 beta show
            they do understand that.
            I have used the following PHP test page encoded as ISO-8859-7:

            <?php
            $text = 'Ποιος είναι Θεός;';
            $xmlSource = '<?xml version="1.0" encoding="ISO-8859-7"?>' . "\r\n";
            $xmlSource .= '<text xml:lang="gr">' . $text . '</text>';
            header('Content-Type: application/xml');
            echo $xmlSource;
            ?>

            Then client-side script reads the XML sent as follows:

            function getText (url, callback) {
            var httpRequest;
            if (typeof XMLHttpRequest != 'undefined') {
            httpRequest = new XMLHttpRequest( );
            }
            else if (typeof ActiveXObject != 'undefined') {
            httpRequest = new ActiveXObject(' Microsoft.XMLHT TP');
            }
            if (httpRequest) {
            httpRequest.ope n('GET', url, true);
            httpRequest.onr eadystatechange = function () {
            if (httpRequest.re adyState == 4 && httpRequest.res ponseXML) {
            var xmlDocument = httpRequest.res ponseXML;
            if (xmlDocument.do cumentElement &&
            xmlDocument.doc umentElement.fi rstChild) {
            callback(xmlDoc ument.documentE lement.firstChi ld.nodeValue);
            }
            }
            };
            httpRequest.sen d(null);
            }
            }

            function insertTextIntoB ody (text) {
            var p;
            if (document.creat eElement && (p = document.create Element('p'))) {
            p.appendChild(d ocument.createT extNode(text));
            document.body.a ppendChild(p);
            }
            }

            getText('test20 04123101.php', insertTextIntoB ody);

            and then the tested browsers show

            Ποιος είναι Θεός;

            in the HTML page.


            Or use the PHP extension iconv if you have that available

            to convert your 8-bit PHP strings to UTF-8 as needed.

            --

            Martin Honnen

            Comment

            • Vasilis Dimos

              #7
              Re: XMLHTTP



              Hey Guys,
              Thanks a lot for the answers. This is actually a lot more than I could
              hope for. I will try this first thing in 2005. Happy new Year to all of
              you.

              Thanks Vasilis

              *** Sent via Developersdex http://www.developersdex.com ***
              Don't just participate in USENET...get rewarded for it!

              Comment

              Working...