SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Finland
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    & character issue with PHP (invalid markup)

    I have a script to make left and right arrow key presses to go to next/previous picture in a photo album.

    PHP Code:
    function checkKeycode(e) {
       var keycode;
       if (window.event) keycode = window.event.keyCode;
       else if (e) keycode = e.which;
       if (keycode == "37") {
          window.location = "<?php if($id != 1) {printf("?id=%d&k=%s#content",$id-1,$k);} ?>"
       }
       else if (keycode == "38") {
          window.location = "kuvat.php"
       }
       else if ((keycode == "39") || (keycode == "32")) {
          window.location = "<?php if($id != $totalNrPics) {printf("?id=%d&k=%s#content",$id+1,$k);} ?>"
       }
    }
    document.onkeydown = checkKeycode
    First I had this inside head-tags in HTML but "&" makes it invalid XHTML 1.0 Strict markup. &amp; would make it valid but then the URLs contain them also and not work (kuvat.php?id=1&amp;k=p etc.). So I figured out to put the javascript code to external file so validator wouldn't see it but because I need to use PHP in the code (to find out the ids of the next/prev pictures) I must name the external file checkKeycode.php and then it's not possible to include in HTML as javascript:

    HTML Code:
    <script type="text/javascript" src="inc/checkKeycode.???"></script>
    So in summary: the javascript works fine but it's invalid markup because of the "&" character and I want it valid. Putting the code to external file would solve it but then it's not possible to use PHP.

    Am I correct and any ideas what should I do to fix it?

    Thanks for the help

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use a CDATA section. Eg.:
    PHP Code:
    <script>
    //<![CDATA[
    function checkKeycode(e) {
       var keycode;
       if (window.event) keycode = window.event.keyCode;
       else if (e) keycode = e.which;
       if (keycode == "37") {
          window.location = "<?php if($id != 1) {printf("?id=%d&k=%s#content",$id-1,$k);} ?>"
       }
       else if (keycode == "38") {
          window.location = "kuvat.php"
       }
       else if ((keycode == "39") || (keycode == "32")) {
          window.location = "<?php if($id != $totalNrPics) {printf("?id=%d&k=%s#content",$id+1,$k);} ?>"
       }
    }
    document.onkeydown = checkKeycode
    //]]>
    </script>

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Finland
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes! Thanks a lot.


Bookmarks

Posting Permissions

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