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

    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
    Join Date
    May 2002
    Posts
    10,943

    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 03:19 PM.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3

    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
    Join Date
    May 2002
    Posts
    10,943

    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

    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
    Join Date
    May 2002
    Posts
    10,943

    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

    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
    Join Date
    May 2002
    Posts
    10,943

    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

    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 04:55 PM.
    --
    Synetech

  10. #10
    Join Date
    May 2002
    Posts
    10,943

    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

    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

    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
    Join Date
    May 2002
    Posts
    10,943

    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
  •  





Click Here to Expand Forum to Full Width

Featured