开发者

PHP within Javascript?

I wanted to include some PHP inside of some Javascript code to edit a div's innerHTML.

When I use the code

$content = file($filename);
$name = $content[0];
<script type="text/javascript">
function showMoon1()
{
var MD = document.getElementById('m1t');
MD.innerHTML = "<font face='Georgia'><table border="0"><tr><th><?php echo $name; ?></th></tr><tr><td>Rating: rating</td></tr><tr><td>Review: review</td></tr&开发者_开发问答gt;<tr><td>Category: category</td></tr><tr><td>Link: <a href='http://youtube.com'>welcome</a></td></tr></table></font>";
}
</script>
<div id="m1t"></div>
<a href="#" onClick="showmoon1()">CLICK HERE</a>

For some reason, When I click the "CLICK HERE" link, nothing happens. I edited the full code down to a short example, so I don't know if the error comes up. All I know is when I use a PHP variable with the content[0]; code, it doesn't work. When I tried $name = "hi"; It works. What?

Please don't vote this question down because it's specific. Thank you.


You're missing a closing php tag before your script:

...
$content = file($filename);
$name = $content[0];
// You were missing this tag
?> 
<script type="text/javascript">
function showMoon1()
{
var MD = document.getElementById('m1t');
MD.innerHTML = "<font face='Georgia'><table border="0"><tr><th><?php echo $name; ?></th></tr><tr><td>Rating: rating</td></tr><tr><td>Review: review</td></tr><tr><td>Category: category</td></tr><tr><td>Link: <a href='http://youtube.com'>welcome</a></td></tr></table></font>";
}
</script>
<div id="m1t"></div>
<a href="#" onClick="showmoon1()">CLICK HERE</a>


PHP is server-side, Javascript is client-side. You need to handle them accordingly. All PHP code needs to be wrapped in

<?php
...
?>


  • Keep javascript in separate file.
  • Do not attach events fin HTML , you can do that in above mentioned JS file.

And instead of this madness , you should use XHR for javascript to request custom content from php script.


On click you are calling showmoon1() and you have defined showMoon1(). Both function names dont match.. Also the innerhtml string gets closed in the middle due to some double quotes in b/w at: border="0" change it to border='0'

 <script type="text/javascript">
 function showmoon1()
 {
 alert(1);
 var MD = document.getElementById('m1t');
 MD.innerHTML = "<font face='Georgia'><table border='0'><tr><th><?php echo $name; ?></th></tr><tr><td>Rating: rating</td></tr><tr><td>Review: review</td></tr><tr><td>Category: category</td></tr><tr><td>Link: <a href='http://youtube.com'>welcome</a></td></tr></table></font>";
 }
 </script>


<?php
$content = file($filename);
$name = $content[0];
?>
<script type="text/javascript">
function showMoon1()
{
var MD = document.getElementById('m1t');
MD.innerHTML = "<font face='Georgia'><table border="0"><tr><th><?php echo $name; ?></th></tr><tr><td>Rating: rating</td></tr><tr><td>Review: review</td></tr><tr><td>Category: category</td></tr><tr><td>Link: <a href='http://youtube.com'>welcome</a></td></tr></table></font>";
}
</script>
<div id="m1t"></div>
<a href="#" onClick="showmoon1()">CLICK HERE</a>

is more correct

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜