admin管理员组

文章数量:1425774

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><tr><td>Category: category</td></tr><tr><td>Link: <a href=''>wele</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 es 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.

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><tr><td>Category: category</td></tr><tr><td>Link: <a href='http://youtube.'>wele</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 es 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.

Share Improve this question asked Aug 27, 2011 at 20:47 snarkyt123snarkyt123 691 gold badge2 silver badges6 bronze badges 4
  • 7 You can't mix PHP and JavaScript this way. You need to wrap the PHP in <?php ?> symbols – Pekka Commented Aug 27, 2011 at 20:48
  • What do you see if you try var_dump($content) as the third line in that script? – gargantuan Commented Aug 27, 2011 at 20:52
  • 5 -1 no closing and opening ( ?>, <?php ) tags , use of `<font> tags and tables for layout in 21st century, newline before { in JS. – tereško Commented Aug 27, 2011 at 21:02
  • Today the "onclick" attribute is usually written with lowercase letters only. – migu Commented Aug 27, 2011 at 21:56
Add a ment  | 

5 Answers 5

Reset to default 2

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.'>wele</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.'>wele</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.'>wele</a></td></tr></table></font>";
}
</script>
<div id="m1t"></div>
<a href="#" onClick="showmoon1()">CLICK HERE</a>

is more correct

本文标签: PHP within JavascriptStack Overflow