douwei7471 2019-07-17 16:28
浏览 199
已采纳

为什么PHP echo会返回完整标记的html,而不仅仅是echo中的内容?

I have an html page with some text inputs, some jquery that uses ajax to post the data from those inputs, and a php script to do some stuff with the input data. The php script then returns some data. The alert, however, is including a bunch of html markup that I don't want to be returned. I'm not sure why it's doing this.

My HTML:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <link rel="stylesheet" type="text/css" href="style.css">
    <title>HTML Form</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>
    <input type="text" name="example" id="example">
    <button type="button" class="btn btn-primary" id="proceed">Proceed</button>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="http://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
    <script src="code.js"></script>
</body>

code.js:

$(document).ready(function () {
    $("#proceed").click(function () {
        var request = $.post("script.php", { // Post input to php
            formData: $("#example").val()
        }, function (response) {
            console.log(response);
        });
    });
});

script.php:

<!DOCTYPE html>
<html>

<body>

    <?php
    $example_input = isset($_POST["formData"]) ? $_POST["formData"] : null;
    $keywords = preg_split("/[\s,]+/", $example_input);
    echo json_encode($keywords);
    ?>
</body>

</html>

If I enter the value "xyz" into the input what ends up getting logged to the console is:

<!DOCTYPE html>
<html>

<body>

    ["xyz"]</body>

</html>

Why is this happening and how can I get only the value within the brackets? Thanks!

  • 写回答

2条回答 默认 最新

  • douyi3833 2019-07-17 16:30
    关注

    That's the way PHP works. Anything outside of <?php ... ?> is output normally. This is how you mix static HTML (or any other language) with dynamic results.

    A script that's just supposed to return JSON should not contain any HTML code before or after it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?