dongyue0263 2013-02-09 11:55
浏览 40
已采纳

为什么我的PHP页面会响应整个HTML源代码,而不仅仅是我正在回应的数据?

I am trying to communicate back and forth with the server with the help of jQuery.

This is my simple scenario:

  1. Send data through html form using jQuery post method.
  2. Receive data on the server side end echo it back again.
  3. Receive same data on the client side in the form of jQuery callback and do something with data (console.log in the broser)

Now my problem is that php is returning source html code with tags, instead of data that has been received ? So if I type "Hello" in the form field on the client side, php is returning whole html source from the page. Why is that ?

Html:

<form action="#">
     <textarea name="content" id="content" rows="8" cols="40"></textarea>
        <p><button>Click to submit</button></p>
    </form>

jquery:

    (function(){
            $("form").on("submit",function(e){
                $.post("save.php",$(this).serialize(),function(data){
                    alert(data);
                })
            e.preventDefault();
            })
    }());

PHP:

<!DOCTYPE HTML>
<html charset="utf-8">
    <head>
        <title>Index</title>
        <link rel="stylesheet" href="path-to-stylesheet.css" type="text/css"/>
    </head>
    <body>
        <?php
            echo $_POST['content'];
        ?> 
    <script src=""></script>
    </body>
</html>
  • 写回答

2条回答 默认 最新

  • drfm55597 2013-02-09 12:13
    关注

    You are sending a complete html page back to the browser, that's why you don't receive only the data. Change your php file to the following and it should work as expected:

    <?php
    echo $_POST['content'];
    ?>
    

    Why did PHP return a whole html page in your case?

    1. PHP parses the whole save.php file
    2. If there are no <?php ?> tags, the whole content is left untouched and will be sent to the browser as it is
    3. If it finds any <?php ?> tags the parser interprets the code and outputs it in the same place

    Example

    save.php

    <!DOCTYPE HTML>
    <html charset="utf-8">
        <head>
            <title>Index</title>
            <link rel="stylesheet" href="path-to-stylesheet.css" type="text/css"/>
        </head>
        <body>
            <?php
                echo $_POST['content'];
            ?> 
        <script src=""></script>
        </body>
    </html>
    

    The parser finds one <?php ?> tag:

            <?php
                echo $_POST['content'];
            ?> 
    

    And interprets it: (simply assuming that $_POST['content'] == 'john smith')

            john smith
    

    Now, the interpreted code gets inserted and replaces the php tag in the original content:

    <!DOCTYPE HTML>
    <html charset="utf-8">
        <head>
            <title>Index</title>
            <link rel="stylesheet" href="path-to-stylesheet.css" type="text/css"/>
        </head>
        <body>
            john smith
        <script src=""></script>
        </body>
    </html>
    

    Finally, this is sent to the browser and gets processed by the jQuery Ajax callback.

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

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)