如何使用JavaScript函数在网页中显示PHP变量


How do I use a JavaScript function to display a PHP variable in my webpage?

如何使用JavaScript函数(通过按钮"点击事件")在网页中显示PHP变量?

当我在函数中使用文字文本字符串时,它工作得很好,但当我用PHP echo命令(<?php echo $test; ?>)替换该文本时,我不会得到任何返回。请问我做错了什么?

下面是我尝试做的一个例子:

<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>

您在对$test进行echo'd之后设置它,因此它是空的,因此它为空。。。