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>