-3

Possible Duplicate:
Pass a PHP string to a Javascript variable (including escaping newlines)

I got this problem rendering or making a PHP code functional using jQuery. huhu.

Can someone help me?

Here's the code I wanted to do:

document.getElementById("fade").innerHTML = '<?php echo 'SOME-PHP-CODE-HERE'; ?>
Community
  • 1
  • 1
Roberto Becher
  • 164
  • 2
  • 4
  • 11

3 Answers3

1

Try using

document.getElementById("fade").innerHTML = '<?php echo $var; ?>';

Check if $var has no quotes. If it has, you need to escape them using addSlashes()

And if you are using JQuery you can better write

$('#fade').html('<?php echo $var; ?>');
Ananth
  • 4,227
  • 2
  • 20
  • 26
  • @RobertoBecher: You really should work on articulating your questions. What you wanted was not " _Render a PHP code using jQuery_ ", but " _Pass PHP variable to JavaScript_ " (which would be a duplicate of [this question](http://stackoverflow.com/questions/168214/pass-a-php-string-to-a-javascript-variable-including-escaping-newlines)). – Tadeck Nov 05 '11 at 06:26
0

Try: document.getElementById("fade").innerHTML = '<?=$php-variable;?>

Make sure it is embedded javascript and not external. (Between <script></script> tags in the .php file)

switz
  • 24,384
  • 25
  • 76
  • 101
0

It is hard to tell, what you are asking for. But assuming you want to display some PHP code within some element and you want to do this from JavaScript, this is some solution for you:

Download PHP.js script and place it on your site, then include it so it is available from other JavaScript code.

Second, use htmlentities() function on the content you want to show (the string containing PHP code). This may look like the following:

var phpCode = '<?php echo \'something goes here\'; ?>';
document.getElementById('fade').innerHTML = htmlentities(phpCode);

As a proof that it works, see this jsfiddle.

Tadeck
  • 132,510
  • 28
  • 152
  • 198