FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Can't I put javascript in PHP echo <<< ?





takashiro
Code:
<?php
if(!defined('IN_DISCUZ')) {
   exit('Access Denied');
}
$roomid = $_GET['id'];
$rwidth = "634";
$rheight = "476";

echo <<< EOT
<html>
<script language="JavaScript">
var shanliao_roomid =$roomid;
<!--{if $discuz_uid}-->
var shanliao_username = "$discuz_user";
<!--{else}-->
var shanliao_username = "";
<!--{/if}-->
function shanliao_chat(obj)
{
obj.href += shanliao_roomid + "\<!--{if $discuz_uid}-->?username=$discuz_userss<!--{/if}-->";
window.open(obj.href, '_shanliaoWnd_07', 'width=$rwidth,height=$rheight, resizable=1');
return false;
}
</script>
</head>
<body>
<span>| <a href="http://www.shanliao.com" target="_blank"  onClick="shanliao_chat(this); return false;">CHAT</a></span>
<embed src="http://www.shanliao.com/b1359.swf" type="application/x-shockwave-flash"  width="634" height="476" allowScriptAccess="always"></embed>
</body>
</html>
EOT;
?>


When I put javascript in echo <<<, it does not work at all.
blueray
If I have take out the javascript code, then, and added a simple callback function to it.
It works without any issues.
Please check the variables that is generated from server-side be completely.


Code:
<?php
if(!defined('IN_DISCUZ')) {
   exit('Access Denied');
}
$roomid = $_GET['id'];
$rwidth = "634";             /* Also be good use single quote if it is not do variable parsing */
$rheight = "476";

echo <<< EOT
<html>
<script language="JavaScript">  /* It is much better changed to type="text/javascript" */
function callback()
{
    alert('callback func');
}
</script>
</head>
<body>
<span>| <a href="http://www.shanliao.com" target="_blank"  onClick="shanliao_chat(this); return false;">CHAT</a></span>
<embed src="http://www.shanliao.com/b1359.swf" type="application/x-shockwave-flash"  width="634" height="476" allowScriptAccess="always"></embed>
<script type="text/javascript">
callback();
</script>
</body>
</html>
EOT;
?>
AftershockVibe
There's not reason why you shouldn't be able to echo Javascript. PHP can't tell the different between HTML, Java, CSS or anything else... it's just text.

I've never used your method of using "EOT" to print though. However, what is most likely to have happenned is some confusion with your use of double quotes or variable substitiution.

Try escaping your quote marks in the printed text: \" and \' instead of just " and '.
An easy way to see where things have gone wrong is just to view the source of the non-working page and work out how much echo actually printed before it fell over.

Very Happy
blueray
Hi, AftershockVibe

But it does't seem be the issues with the quotation mark " or '
As my example function `callback`:

Code:
function callback()
{
    alert('callback func');
}


It includes the quotation also.
And the heredoc string format should support using " and '
So, I think the issues was the <!-- if --> server-side template
parsing command.

My I right ?
Stubru Freak
Should be
Code:
echo <<<EOT


No space between <<< and EOT.
Related topics
PHP Toturail
Php and Other Wonderful Newbie Stories for Help.
Image in title bar
where i put my PHP file
Can someone help me??
Including Rank
I havent got my account handled nor tested this code...
PHP using Sessions
Need help: Error in Wordpress file
php tutorial problem
Editing files online
Parsing PHP in HTML
I could use some help with dropdown lists in php
Your-IP
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.