简体   繁体   English

您如何在PHP中使用echo在HTML中打印文本?

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

I am attempting to use php to print out a line of text onto a html file, but when I run the html, it merely prints the word undefined. 我正在尝试使用php在html文件上打印出一行文本,但是当我运行html时,它只会打印未定义的单词。 How can I fix this? 我怎样才能解决这个问题? Here is the code: 这是代码:

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:

<?php
echo "Hello, World"
?>

As commented your usage of XMLHttpRequest is wrong, you should check online example of it, like this example from the mozilla dev network 如您所评论的那样,您对XMLHttpRequest的使用是错误的,您应该在线查看它的示例, 例如来自mozilla dev网络的示例

Adapted to your script : 适应您的脚本:

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

this script does not account for the errors from the request . 该脚本无法解决请求中错误 Please read the documentation to handle them. 请阅读文档以进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM