dsagzmosl32217092 2015-03-18 19:03
浏览 45
已采纳

你如何在PHP中使用echo在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. How can I fix this? Here is the code:

HTML:

  1. <html>
  2. <head>
  3. </head>
  4. <body>
  5. <script type="text/javascript">
  6. var xmlhttp=new XMLHttpRequest();
  7. var textinhtml=document.createElement("div");
  8. var text=document.createTextNode(xmlhttp.open("GET","http://mikeyrichards.bugs3.com/test.php",true));
  9. textinhtml.appendChild(text);
  10. document.body.appendChild(textinhtml);
  11. xmlhttp.send();
  12. </script>
  13. </body>

PHP:

  1. <?php
  2. echo "Hello, World"
  3. ?>
  • 写回答

1条回答 默认 最新

  • dsh125986083 2015-03-18 19:19
    关注

    As commented your usage of XMLHttpRequest is wrong, you should check online example of it, like this example from the mozilla dev network

    Adapted to your script :

    1. <script type="text/javascript">
    2. var xmlhttp=new XMLHttpRequest();
    3. xmlhttp.onload = function() {
    4. var textinhtml=document.createElement("div");
    5. var text=document.createTextNode(this.responseText);
    6. textinhtml.appendChild(text);
    7. document.body.appendChild(textinhtml);
    8. }
    9. xmlhttp.open("GET","http://mikeyrichards.bugs3.com/test.php",true)
    10. xmlhttp.send();
    11. </script>

    this script does not account for the errors from the request. Please read the documentation to handle them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部