I want to echo the variable when I use test(); fuction. I meanwhen I use the variable with this function like test(john) , how to use it in that php snippet. Anyone knows ? <script type="text/javascript"> function test(){ document.getElementById("php_code").innerHTML=" <?php echo "hello"; ?> "; } </script> PHP:
<script type="text/javascript"> function test(name){ document.getElementById("php_code").innerHTML= name; } </script> <div id="php_code"></div> HTML:
ohh gotcha well the code that you wrote should work just fine. However if you're trying to change the php variable after the page has been loaded this isn't possible. All php variables are created before the page loads and can't be altered unless you reload the page.
I know, why it doesn't work. There is a line break after the double quotes. Now, it'll work document.getElementById("php_code").innerHTML = "<?php echo "hello"; ?>";