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.
-
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
5 Answers
Reset to default 2You'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
版权声明:本文标题:PHP within Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745451740a2658918.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论