Web Hosting Talk







View Full Version : javascript dies in php


ti_nhatrang
01-17-2008, 06:49 AM
Hi guys,

I can't seem to put this in a echo statement in php, here's what I got and it doesn't seem to work. Any kind of help would be greatly appreciated.


echo "<script type='text/javascript'><!--//<![CDATA[
var m3_u = (location.protocol=='https:'?'https://localhost/www/delivery/ajs.php':'http://localhost/www/delivery/ajs.php');
var m3_r = Math.floor(Math.random()*99999999999);
if (!document.MAX_used) document.MAX_used = ',';
document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
document.write ("?zoneid=5&amp;block=1");
document.write ('&amp;cb=' + m3_r);
if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used);
document.write ("&amp;loc=" + escape(window.location));
if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));
if (document.context) document.write ("&context=" + escape(document.context));
if (document.mmm_fo) document.write ("&amp;mmm_fo=1");
document.write ("'><\/scr"+"ipt>");
//]]>--></script><noscript><a href='http://localhost/www/delivery/ck.php?n=a00e4050&amp;cb=INSERT_RANDOM_NUMBER_HERE' target='_blank'><img src='http://localhost/www/delivery/avw.php?zoneid=5&amp;n=a00e4050' border='0' alt='' /></a></noscript>";



Thanks again in advance.

foobic
01-17-2008, 07:45 AM
You have quotes in the javascript you're trying to output. Either escape them (all of them!) or preferably use a heredoc:

echo <<<ENDJS
<script type='text/javascript'><!--//<![CDATA[
var m3_u = (location.protocol=='https:'?'https://localhost/www/delivery/ajs.php':'http://localhost/www/delivery/ajs.php');
var m3_r = Math.floor(Math.random()*99999999999);
if (!document.MAX_used) document.MAX_used = ',';
document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
document.write ("?zoneid=5&amp;block=1");
document.write ('&amp;cb=' + m3_r);
if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used);
document.write ("&amp;loc=" + escape(window.location));
if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));
if (document.context) document.write ("&context=" + escape(document.context));
if (document.mmm_fo) document.write ("&amp;mmm_fo=1");
document.write ("'><\/scr"+"ipt>");
//]]>--></script><noscript><a href='http://localhost/www/delivery/ck.php?n=a00e4050&amp;cb=INSERT_RANDOM_NUMBER_HERE' target='_blank'><img src='http://localhost/www/delivery/avw.php?zoneid=5&amp;n=a00e4050' border='0' alt='' /></a></noscript>
ENDJS;

isurus
01-17-2008, 07:50 AM
Did you check your error log?

document.write ("<scr"You are using double quotes within your string - this will be terminating the string prematurely.

You could use heredoc syntax (http://uk2.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc):
echo <<<BOB
<script type='text/javascript'><!--//<![CDATA[
var m3_u = (location.protocol=='https:'?'https://localhost/www/delivery/ajs.php':'http://localhost/www/delivery/ajs.php');
var m3_r = Math.floor(Math.random()*99999999999);
if (!document.MAX_used) document.MAX_used = ',';
document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
document.write ("?zoneid=5&amp;block=1");
document.write ('&amp;cb=' + m3_r);
if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used);
document.write ("&amp;loc=" + escape(window.location));
if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));
if (document.context) document.write ("&context=" + escape(document.context));
if (document.mmm_fo) document.write ("&amp;mmm_fo=1");
document.write ("'><\/scr"+"ipt>");
//]]>--></script><noscript><a href='http://localhost/www/delivery/ck.php?n=a00e4050&amp;cb=INSERT_RANDOM_NUMBER_HERE' target='_blank'><img src='http://localhost/www/delivery/avw.php?zoneid=5&amp;n=a00e4050' border='0' alt='' /></a></noscript>
BOB;Do you actually need to get php to echo that data? There don't seem to be any PHP variables in there, so why not just close your php tag, output the JS, then open your php tag again:
?>
<script type='text/javascript'><!--//<![CDATA[
var m3_u = (location.protocol=='https:'?'https://localhost/www/delivery/ajs.php':'http://localhost/www/delivery/ajs.php');
var m3_r = Math.floor(Math.random()*99999999999);
if (!document.MAX_used) document.MAX_used = ',';
document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
document.write ("?zoneid=5&amp;block=1");
document.write ('&amp;cb=' + m3_r);
if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used);
document.write ("&amp;loc=" + escape(window.location));
if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));
if (document.context) document.write ("&context=" + escape(document.context));
if (document.mmm_fo) document.write ("&amp;mmm_fo=1");
document.write ("'><\/scr"+"ipt>");
//]]>--></script><noscript><a href='http://localhost/www/delivery/ck.php?n=a00e4050&amp;cb=INSERT_RANDOM_NUMBER_HERE' target='_blank'><img src='http://localhost/www/delivery/avw.php?zoneid=5&amp;n=a00e4050' border='0' alt='' /></a></noscript>
<?php

isurus
01-17-2008, 07:53 AM
hmm.. looks like I was too slow to post :)

ti_nhatrang
01-17-2008, 05:50 PM
I've tried both method and it doesn't seem to work, here is the whole section that I'm trying to do:


if($count == 4) {

echo " <div style=\"float:left;height:300px;border:2px solid #ffffff;background-color:#000000;background-repeat:no-repeat;text-align:left;z-index:1;\"><script type='text/javascript'><!--//<![CDATA[
var m3_u = (location.protocol=='https:'?'https://localhost/www/delivery/ajs.php':'http://localhost/www/delivery/ajs.php');
var m3_r = Math.floor(Math.random()*99999999999);
if (!document.MAX_used) document.MAX_used = ',';
document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
document.write ("?zoneid=5&amp;block=1");
document.write ('&amp;cb=' + m3_r);
if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used);
document.write ("&amp;loc=" + escape(window.location));
if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));
if (document.context) document.write ("&context=" + escape(document.context));
if (document.mmm_fo) document.write ("&amp;mmm_fo=1");
document.write ("'><\/scr"+"ipt>");
//]]>--></script><noscript><a href='http://localhost/www/delivery/ck.php?n=aa39492f&amp;cb=INSERT_RANDOM_NUMBER_HERE' target='_blank'><img src='http://localhost/www/delivery/avw.php?zoneid=5&amp;n=aa39492f' border='0' alt='' /></a></noscript>
</div>";
#Increase the counter.
$count = 1;
} else {
#This is just to increment the counter.
$count++;
}

ThatScriptGuy
01-17-2008, 06:27 PM
<?php
if($count == 4) {

?>
<div style=\"float:left;height:300px;border:2px solid #ffffff;background-color:#000000;background-repeat:no-repeat;text-align:left;z-index:1;\"><script type='text/javascript'><!--//<![CDATA[
var m3_u = (location.protocol=='https:'?'https://localhost/www/delivery/ajs.php':'http://localhost/www/delivery/ajs.php');
var m3_r = Math.floor(Math.random()*99999999999);
if (!document.MAX_used) document.MAX_used = ',';
document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
document.write ("?zoneid=5&amp;block=1");
document.write ('&amp;cb=' + m3_r);
if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used);
document.write ("&amp;loc=" + escape(window.location));
if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));
if (document.context) document.write ("&context=" + escape(document.context));
if (document.mmm_fo) document.write ("&amp;mmm_fo=1");
document.write ("'><\/scr"+"ipt>");
//]]>--></script><noscript><a href='http://localhost/www/delivery/ck.php?n=aa39492f&amp;cb=INSERT_RANDOM_NUMBER_HERE' target='_blank'><img src='http://localhost/www/delivery/avw.php?zoneid=5&amp;n=aa39492f' border='0' alt='' /></a></noscript>
</div>

<?php
#Increase the counter.
$count = 1;
} else {
#This is just to increment the counter.
$count++;
}

?>

ti_nhatrang
01-17-2008, 06:40 PM
it throws off my alignment if I do what you just recommended.

ThatScriptGuy
01-17-2008, 06:44 PM
Then that's your code throwing off your alignment. All I did was separate your PHP conditional from your HTML code.

ti_nhatrang
01-17-2008, 06:51 PM
ignore that, it's fine now.

ti_nhatrang
01-17-2008, 07:04 PM
Thanks again for all your help! I never thought it could be done this way, which will make things a lot easier from here on out.