...

View Full Version : placing javascript within php



DJ Hands3
03-19-2009, 01:39 AM
Just trying to include some javascript within the print function and is not going to well!



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?

PappaJohn
03-19-2009, 01:59 AM
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.

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

Fou-Lu
03-19-2009, 02:04 AM
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.types.string.php#language.types.string.syntax.heredoc
This one is surprisingly hard to find in the manual since its a construct, not a function.

sea4me
03-19-2009, 03:26 AM
Here :) :

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>";

DJ Hands3
03-19-2009, 09:09 PM
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



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>";

sea4me
03-20-2009, 12:27 AM
Well it was your java that was causing your other problems :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum