tags:

views:

123

answers:

6

Working Example:

This is almost identical to code I use in another places on my page but fails here for some reason.

<?php
//$p = "test";
?>

<script>
alert('posts are firing? ');
parent.document.getElementById('posts').innerHTML = "test";
</script>

Failing example: (alert still works)

<?php
$p = "test of the var";
?>

<script>
alert('posts are firing? ');
parent.document.getElementById('posts').innerHTML = '<?php $p; ?>';
</script>
+2  A: 

Debugging 101: Start checking all variable values.

alert(parent);
alert(parent.document);
alert(parent.document.getElementById('posts'));

as well as the value rendered by: '<?php $p; ?>'

Kon
I have worked it back to the $p variable. Thanks.
ian
+6  A: 

Try

'<?php echo $p; ?>';

or

'<?= $p ?>';
NinethSense
Remember: You have to enable short_tags in your php.ini file for <?= to work :)
Nolte Burke
Yes, that is why I provided both lines :-)
NinethSense
+2  A: 

Make sure your 'posts' object (I guess it is DIV or SPAN) loads before you fill it using javascript.

div is already loaded... but thanks.
ian
+1  A: 

You're trying to generate javascript with php, here I use a simple echo:

<?php

$p = "test of the var";

echo"
<div id='posts'></div>

<script type='text/javascript'>
var posts = document.getElementById('posts');
posts.innerHTML = '$p';
</script>
";

?>

Note the $p and that the div is printed before the javascript!

MrHus
A: 

You are not outputting the variable data is why it isn't working. You need to echo or print the variable $p.

Joe Bubna
A: 

In your example the $p is being evaluated, not printed.

To print it you should use print, echo, or the syntax <\?=$p;?>. without the \

Andrea Ambu