-1

How do I use a JavaScript function (via button "click event") to display a PHP variable in my web page?

When I use a literal text string in my function, it works fine, but when I replace that text with a PHP echo command (<?php echo $test; ?>), I get nothing returned. What am I doing wrong, please?

Here is an example of what I am trying to do:

<html>
<head>
<script>
function myFunction() {
    document.getElementById("test").innerHTML = "<?php echo $test; ?>";
}
</script>
</head>
<body>
<?php $test = "success"; ?>
<p>Click the button to trigger a function.</p>

<button onclick="myFunction()">Export</button>

<p id="test"></p>

</body>
</html>
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

1

You're setting $test after it's being echo'd so it's empty, thus the blank...

dbinns66
  • 790
  • 6
  • 7
  • I'm not sure that's the issue, because even when I add data to the variable BEFORE the echo statement, the JavaScript function doesn't execute the PHP echo command. – PeterTodd Mar 30 '15 at 18:46