【问题标题】:PHP returns whyever HTML tagsPHP 返回任何 HTML 标签
【发布时间】:2018-05-26 12:59:42
【问题描述】:

好的,我有这个 php 代码:

 <!doctype html>
<html>
  <body>
    <?php
      $q = intval($_GET['q']);
      $con = mysqli_connect('localhost', 'root', '', 'testDB');
      if(!$con){
        die('Could not connect: '. mysqli_error($con));
      }

      mysqli_select_db($con, "testDB");
      $query = "SELECT * FROM `aTable`;";
      $result = mysqli_query($con,$query);
      $row = mysqli_fetch_array($result);
      echo $row[$q];
      mysqli_close($con);
      exit();
     ?>
  </body>
</html>

我用 HTML 调用这个脚本:

 if(window.XMLHttpRequest){
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if(this.readyState == 4 && this.status == 200){
            var Response = this.responseText;
            console.log(Response);
        }
    };
    xmlhttp.open("GET", "script.php?q=1", true);
    xmlhttp.send();
}

但是,控制台记录

<!doctype html>
<html>
  <body>
    -3, -1, -2, 0, 2, 1, 2, 4, 5, 3, 4, 2, 4, 5, 6, 2, 3, 1, 3, 4, 1, -1, -3, -1

我不知道为什么,但它返回了这些我不想要的 HTML 标记(只有从 -3 到 -1 [带逗号] 的值)

如何删除(未闭合的)HTML 标签?

感谢所有回答!

【问题讨论】:

  • 从您的文件中删除 HTML 标记。当然。
  • 在文本编辑器中打开您的 PHP 文件。删除你不想要的。保存文件。 (注意:标签是“未关闭的”,因为您调用 exit() 在关闭标签之前终止脚本。)
  • 真的有这样的问题吗?

标签: php html xmlhttprequest


【解决方案1】:

当您通过 ajax 调用 script.php 时,响应将包含您文件中的所有 html 标签,请确保将它们全部删除。此外,您可能希望使用json_encode 函数将 php 对象作为字符串发送到客户端,这是一个示例:

script.php

<?php
    $q = intval($_GET['q']);
    $con = mysqli_connect('localhost', 'root', '', 'testDB');
    if(!$con){
        die('Could not connect: '. mysqli_error($con));
    }

    mysqli_select_db($con, "testDB");
    $query = "SELECT * FROM `aTable`;";
    $result = mysqli_query($con,$query);
    $row = mysqli_fetch_array($result);
    echo json_encode($row[$q], true);
    mysqli_close($con);
    exit();

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 2011-04-25
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 2018-02-13
    相关资源
    最近更新 更多