PHP返回whyever HTML标记

PHP返回whyever HTML标记,php,html,xmlhttprequest,Php,Html,Xmlhttprequest,好的,我有以下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));

好的,我有以下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>
不管怎样,控制台会记录

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

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


谢谢你的回答

通过ajax调用
script.php
时,响应将包含文件中的所有html标记,请确保将它们全部删除。此外,您可能希望使用函数将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();

从文件中删除HTML标记。当然可以。在文本编辑器中打开PHP文件。移除你不想要的东西。保存文件。(注意:标记是“未关闭的”,因为您调用了
exit()
,它在关闭标记之前终止脚本。)真的这样问吗?
<?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();