PHP dentro de Javascript?

I wanted to include some PHP inside of some Javascript code to edit a div's innerHTML.
Cuando uso el código

$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>

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"; Funciona. ¿Qué?
Please don't vote this question down because it's specific. Thank you.

preguntado el 27 de agosto de 11 a las 20:08

You can't mix PHP and JavaScript this way. You need to wrap the PHP in <?php ?> símbolos -

What do you see if you try var_dump($content) as the third line in that script? -

-1 no closing and opening ( ?>, <?php ) tags , use of `<font> tags and tables for layout in 21st century, newline before { in JS. -

Today the "onclick" attribute is usually written with lowercase letters only. -

5 Respuestas

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>

Respondido 28 ago 11, 00:08

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

<?php
...
?>

Respondido 28 ago 11, 00:08

  • 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.

Respondido 28 ago 11, 00:08

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>

Respondido 28 ago 11, 00:08

<?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>

es mas correcto

Respondido 28 ago 11, 00:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.