如何使用 php 中的 echo 打印 html 中的文本?

How do you use echo in php to print text in html?

我试图使用 php 将一行文本打印到 html 文件中,但是当我 运行 html 时,它只打印了词未定义。我怎样才能解决这个问题?这是代码:

HTML:

<html>
<head>
</head>
<body>
<script type="text/javascript">
var xmlhttp=new XMLHttpRequest();
var textinhtml=document.createElement("div");
var text=document.createTextNode(xmlhttp.open("GET","http://mikeyrichards.bugs3.com/test.php",true));
textinhtml.appendChild(text);
document.body.appendChild(textinhtml);
xmlhttp.send();
</script>
</body>  

PHP:

<?php
echo "Hello, World"
?>

根据评论,您对 XMLHttpRequest 的用法是错误的,您应该检查它的在线示例,例如 this example from the mozilla dev network

适应您的脚本:

<script type="text/javascript">
   var xmlhttp=new XMLHttpRequest();
   xmlhttp.onload = function() {
       var textinhtml=document.createElement("div");
       var text=document.createTextNode(this.responseText);
       textinhtml.appendChild(text);
       document.body.appendChild(textinhtml);
   }
   xmlhttp.open("GET","http://mikeyrichards.bugs3.com/test.php",true)
   xmlhttp.send();
</script>

此脚本不考虑请求中的错误。请阅读文档来处理它们。