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.


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

    <meta charset="UTF-8">
    <link rel="stylesheet" href="" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link rel="stylesheet" href="">
    <link rel="stylesheet" type="text/css" href="style.css">
    <title>HTML Form</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

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

    <script src=""></script>
    <script src=""></script>
    <script src="" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
    <script src="code.js"></script>


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


<!DOCTYPE html>


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


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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥100 python读取速度问题
  • ¥15 stm32f407使用DMA问题
  • ¥15 您好 这个API接口该怎么弄 网站搭建好了 API也有 现在就不知道该怎么填写API 不知道怎么用
  • ¥88 用uniapp写一个多端的程序,用到高德地图,用高德的JSAPI吗?
  • ¥20 关于#c++#的问题:水果店管理系统
  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 2024年全国大学生数据分析大赛A题:直播带货与电商产品的大数据分析 问题5. 请设计一份优惠券的投放策略,需要考虑优惠券的数量、优惠券的金额、投放时间段和投放商品种类等因素。求具体的python代码
  • ¥15 有人会搭建生鲜配送自营+平台的管理系统吗