? Validation and UNICODE Problems
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: ? Validation and UNICODE Problems

  1. #1
    Join Date
    Sep 2000
    Location
    here
    Posts
    102

    Unhappy ? Validation and UNICODE Problems

    Hi, Iím working on a little AJAX app using a PHP script but am having two problems with it:


    (1) My XHTML file wonít validate because of this part:

    Code:
    XMLHttp.send(
    	"action=view"
    	+(form.title.value  == "" ? "" : "&title=" +form.title.value)
    	+(form.artist.value == "" ? "" : "&artist="+form.artist.value)
    	+(form.album.value  == "" ? "" : "&album=" +form.album.value)
    	+(form.year.value   == "" ? "" : "&year="  +form.year.value)
    );
    The validators complain about the ampersands; they say that the entities &title, &artists, &album, and &year are not valid. I tried replacing them with &title, etc. and that does fix the validation, but they no longer register as a parameters in the PHP script.

    Any ideas?


    (2) The PHP script returns some text that is inserted into a DIV on the page from XMLHttp.responseText, but it gets messed up if the returned text has UNICODE characters in it.

    Any ideas?


    Thanks a lot.
    --
    Synetech

  2. #2
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,886

    Re: ? Validation and UNICODE Problems

    You are missing an & behind the "view."

    You really should break up that send command. Try the following.
    Code:
    var theTitle = form.title.value;
    var theArtist = form.artist.value;
    var theAlbum = form.album.value;
    var theYear = form.year.value;
    
    var parameters = "action=view&title=" + theTitle + "&artist=" + theArtist + "&album=" + theAlbum + "&year=" + theYear;
    
    XMLHttp.send(parameters);
    Last edited by PeejAvery; June 20th, 2007 at 04:19 PM.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Sep 2000
    Location
    here
    Posts
    102

    Re: ? Validation and UNICODE Problems

    ďBehind the viewĒ? You didnít put one behind the view either (and Iím pretty sure it isnít necessary, possibly even incorrect).


    In any case, I tried your code and it still doesnít validate; it gives the same errors:
    ďWarning: cannot generate system identifier for general entity "title"Ē
    ďError: general entity "title" not defined and no default entityĒ
    ďError: reference to entity "title" for which no system identifier could be generatedĒ
    --
    Synetech

  4. #4
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,886

    Re: ? Validation and UNICODE Problems

    No. It is not incorrect. To separate parameters you must use &. When I said behind the view I was talking about...action=view. But I see that I wasn't looking far enough down the code.

    Are you sure you have all the AJAX POST headers set up?

    You should have all the following.
    Code:
    obj.open('POST', url, true);
    obj.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    obj.setRequestHeader('Content-length', parameters.length);
    obj.setRequestHeader('Connection', 'close');
    obj.send(parameters);
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Sep 2000
    Location
    here
    Posts
    102

    Re: ? Validation and UNICODE Problems

    Quote Originally Posted by PeejAvery
    No. It is not incorrect. To separate parameters you must use &. When I said behind the view I was talking about...action=view. But I see that I wasn't looking far enough down the code.
    Oh ok, I thought you meant ?action=view, which Iím fairly sure would be correct because ampersands separate parameters and you canít put one before the first one.

    Quote Originally Posted by PeejAvery
    Are you sure you have all the AJAX POST headers set up?

    You should have all the following.
    Code:
    obj.open('POST', url, true);
    obj.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    obj.setRequestHeader('Content-length', parameters.length);
    obj.setRequestHeader('Connection', 'close');
    obj.send(parameters);
    Pretty sure, Iíve got
    Code:
    XMLHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    
    XMLHttp.setRequestHeader("Charset", "UTF-8");  //this was an attempt to fix problem (2); it didnít help
    The first line was all thatís listed in the AJAX texts I got it from.

    Problem (1) isnít an XMLHttpRequest problem anyway, itís a markup problem. The XHTML validators complain about the presence of an ampersand in the string, but they are necessary to pass multiple parameters.

    Also, any reason you set the open command to synchronous mode?
    --
    Synetech

  6. #6
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,886

    Re: ? Validation and UNICODE Problems

    Quote Originally Posted by Synetech
    Problem (1) isnít an XMLHttpRequest problem anyway, itís a markup problem. The XHTML validators complain about the presence of an ampersand in the string, but they are necessary to pass multiple parameters.
    Yes, the markup might be part of the problem, but you said that my example did not work either. That has no markup issues. Can you post the whole page's code?

    Quote Originally Posted by Synetech
    Also, any reason you set the open command to synchronous mode?
    That is just a snippet I had from another piece of code. I used synchronous for control purposes in that script.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  7. #7
    Join Date
    Sep 2000
    Location
    here
    Posts
    102

    Re: ? Validation and UNICODE Problems

    Hereís a really basic page that has the problem:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
      <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
        <title>Problem!!!</title>
        <script type="text/JavaScript">
          //validators complain about this, but you canít replace the ampersands with &amp;
          var postparams="param1=value1&param2=value2";
        </script>
      </head>
      <body>
      </body>
    </html>
    --
    Synetech

  8. #8
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,886

    Re: ? Validation and UNICODE Problems

    Change...
    Code:
    <script type="text/JavaScript">
    To...
    Code:
    <script type="text/javascript">
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  9. #9
    Join Date
    Sep 2000
    Location
    here
    Posts
    102

    Re: ? Validation and UNICODE Problems

    Actually, I fixed problem (1). From the W3C spec for XHTML:

    Code:
    <script type="text/javascript">
    <![CDATA[
    ... unescaped script content ...
    ]]>
    </script>
    Apparently UNICODE characters in JavaScript comments donít validate as valid SGML.

    Now, I just need to figure out why the PHP response isnít UTF-8.
    Last edited by Synetech; June 20th, 2007 at 05:55 PM.
    --
    Synetech

  10. #10
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,886

    Re: ? Validation and UNICODE Problems

    PHP response? utf8_encode() maybe?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  11. #11
    Join Date
    Sep 2000
    Location
    here
    Posts
    102

    Re: ? Validation and UNICODE Problems

    Quote Originally Posted by PeejAvery
    PHP response? utf8_encode() maybe?
    Yup, that worked. The ďtrickĒ was figuring out where to use it. Also, only part of it was fixed, but a quick test revealed that the part that still isnít working is because somehow, the data was originally inserted into the database incorrectly (doesnít really matter though since it was just a temp table for testing).

    They both seem to be fixed now. Thanks.

    However, itís annoying that you canít put any UNICODE characters in the HTML code or in JavaScript strings, you have to encode them which is not convenient.
    --
    Synetech

  12. #12
    Join Date
    Sep 2000
    Location
    here
    Posts
    102

    Re: ? Validation and UNICODE Problems

    Quote Originally Posted by PeejAvery
    PHP response? utf8_encode() maybe?
    Yup, that worked. The ďtrickĒ was figuring out where to use it. Also, only part of it was fixed, but a quick test revealed that the part that still isnít working is because somehow, the data was originally inserted into the database incorrectly (doesnít really matter though since it was just a temp table for testing).

    They both seem to be fixed now. Thanks.

    However, itís annoying that you canít put any UNICODE characters in the HTML code, you have to encode them.
    --
    Synetech

  13. #13
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,886

    Re: ? Validation and UNICODE Problems

    Glad your problems are fixed.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center