Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Feb 2009
    Posts
    90
    Thanks
    5
    Thanked 0 Times in 0 Posts

    placing javascript within php

    Just trying to include some javascript within the print function and is not going to well!

    PHP Code:
    print("<script type="text/javascript" charset="utf-8"  language="javascript">
                      <!--
                        if (navigator.userAgent.match(/AppleWebKit/i) && navigator.userAgent.match(/Mobile/i)) {
                         document.writeln('<div class=box><h1 style=margin:0>Mix options - download or stream this file</h1><h3><p style=border:0>');
                         document.writeln('<a href=mixes/mp3.php?Advert=\".$random_intro[$random].\'&File=\'.$random_audio[$random].\">Play on a mobile device <img border=0 src=images/files/mobiledevice.gif width=18 height=18></a></p></h3></div>');
                          }
                          else {
                         document.writeln('<div class=box><h1 style=margin:0>Mix options - download or stream this file</h1><h3><p style=border:0>');
                         document.writeln('<a href=http://dj-promo.co.uk/mixes/downloadpromo.htm?filename1=\".$mirror[\'url\'].\" onclick=\"return !window.open(this.href,\'\',\'scrollbars=0,height=50,width=50\');\">');
                         document.writeln('Download this mix  <img border=0 src=images/files/download.gif width=18 height=18></a><br /><a href=http://dj-promo.co.uk/cgi-bin/play.m3u?advert=\".$random_text[$random].\'&intro=\'.$random_intro[$random].\'&show=\'.$mirror[\'url\'].\">');
                         document.writeln('Stream this mix  <img border=0 src=images/files/winamp.gif width=18 height=18></a><br /></a></p></h3></div>');
                          }
                      //-->
                    </script>"
    ); 
    keep getting errors can any of you see the problem?

  • #2
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    Quote Originally Posted by DJ Hands3 View Post
    can any of you see the problem?
    Yes, and not trying to be sarcastic, but looking at it in the php tags the problem is color coded for you.

    It's the double-quotes inside the string you are trying to print(). You will need to escape those double-quotes so php doesn't view them as ending/beginning a new string.

    I mentioned the color-coding as a suggestion. Using a developer's editor which provides syntax highlighting is a big help in finding errors such as this.

    Edit: In addition, if you have error reporting turned on (as you should during development), php will display a, usually, helpful message to point you in the right direction
    Last edited by PappaJohn; 03-19-2009 at 01:01 AM. Reason: clearer wording
    John

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Any IDE with colour markup will definitely help.
    I'll suggest actually using HEREDOC instead of print. Since you're using both double and single quotations within you're code block, a heredoc syntax will get around that whole escaping issue.
    Here is a link to heredoc, its easy to use, but you must be 100% certain that you're ending declaration is on a newline with absolutely no whitespace before it.
    http://php.ca/manual/en/language.typ...syntax.heredoc
    This one is surprisingly hard to find in the manual since its a construct, not a function.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    Regular Coder sea4me's Avatar
    Join Date
    Jan 2009
    Location
    Damn, I don't know...
    Posts
    390
    Thanks
    11
    Thanked 28 Times in 27 Posts
    Here :
    PHP Code:
    echo"<script type=\"text/javascript\" charset=\"utf-8\"  language=\"javascript\"> 
                      <!-- 
                        if (navigator.userAgent.match(/AppleWebKit/i) && navigator.userAgent.match(/Mobile/i)) { 
                         document.writeln('<div class=box><h1 style=margin:0>Mix options - download or stream this file</h1><h3><p style=border:0>'); 
                         document.writeln('<a href=mixes/mp3.php?Advert=\".$random_intro[$random].\'&File=\'.$random_audio[$random].\">Play on a mobile device <img border=0 src=images/files/mobiledevice.gif width=18 height=18></a></p></h3></div>'); 
                          } 
                          else { 
                         document.writeln('<div class=box><h1 style=margin:0>Mix options - download or stream this file</h1><h3><p style=border:0>'); 
                         document.writeln('<a href=http://dj-promo.co.uk/mixes/downloadpromo.htm?filename1=\".$mirror[\'url\'].\" onclick=\"return !window.open(this.href,\'\',\'scrollbars=0,height=50,width=50\');\">'); 
                         document.writeln('Download this mix  <img border=0 src=images/files/download.gif width=18 height=18></a><br /><a href=http://dj-promo.co.uk/cgi-bin/play.m3u?advert=\".$random_text[$random].\'&intro=\'.$random_intro[$random].\'&show=\'.$mirror[\'url\'].\">'); 
                         document.writeln('Stream this mix  <img border=0 src=images/files/winamp.gif width=18 height=18></a><br /></a></p></h3></div>'); 
                          } 
                      //--> 
              </script>"


  • #5
    New Coder
    Join Date
    Feb 2009
    Posts
    90
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I think you got me on the right track sea4me, but that did give me errors still i'v cracked it now this is what works for me

    PHP Code:
    echo"<script type=\"text/javascript\" charset=\"utf-8\"  language=\"javascript\">
                      <!--
                        if (navigator.userAgent.match(/AppleWebKit/i) && navigator.userAgent.match(/Mobile/i)) {
                         document.writeln('<div class=box><h1 style=margin:0>Mix options - download or stream this file</h1><h3><p style=border:0>');
                         document.writeln('<a href=mixes/mp3.php?Advert=$random_intro[$random]&File=$random_text[$random]>Play on a mobile device');
                         document.writeln('<img border=0 src=images/files/mobiledevice.gif width=18 height=18></a></p></h3></div>');
                          }
                          else {
                         document.writeln('<div class=box><h1 style=margin:0>Mix options - download or stream this file</h1><h3><p style=border:0>');
                         document.writeln('<a href=http://dj-promo.co.uk/mixes/downloadpromo.htm?filename1=$mirror[url] onclick=\"return !window.open(this.href,\'\',\'scrollbars=0,height=50,width=50\');\">');
                         document.writeln('Download this mix  <img border=0 src=images/files/download.gif width=18 height=18></a><br />');
                         document.writeln('<a href=http://dj-promo.co.uk/cgi-bin/play.m3u?advert=$random_text[$random]&intro=$random_intro[$random]&show=$mirror[url]\>');
                         document.writeln('Stream this mix  <img border=0 src=images/files/winamp.gif width=18 height=18></a><br />');
                         document.writeln('</p></h3></div>');
                          }
                      //-->
              </script>"

    Last edited by DJ Hands3; 03-19-2009 at 08:56 PM.

  • #6
    Regular Coder sea4me's Avatar
    Join Date
    Jan 2009
    Location
    Damn, I don't know...
    Posts
    390
    Thanks
    11
    Thanked 28 Times in 27 Posts
    Well it was your java that was causing your other problems


  •  

    Posting Permissions

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