doutui6241 2017-01-21 09:47
浏览 15

PHP没有回应ajax发送的数据

do you know whats wrong with my code? I thought it would work. I just want to echo the data from input to div without refreshing website. Ajax sent data, but php didnt show them and i really dont understand why.

<!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script>
        $(document).ready(function() 
        {
            $(document).on("submit", ".form", function()
            {
                event.preventDefault();
                $.ajax({
                    type: 'POST',
                    url: 'http://dev.nostools.cz/',
                    data: $('.form').serialize(),
                    success: function () {
                      //alert('form was submitted');
                    }
                  });
            })
        });

    </script>
    <div style="width: 250px; height: 100px;">
        <div id="show" style="width: 100%; height: 80%; overflow-y: scroll; border: 1px solid black;">
        <?php

            if (isset($_POST['submit']))
            {
                echo $_POST['console']; //echo data or do something here
            }

        ?>
        </div>
        <div style="width: 100%; height: 20%;">
            <form class="form" method="post">
                <input id="console" style="padding: 0; width: 67%;" type="text" name="console" />
                <input style="padding: 0; width: 31%;" type="submit" name="submit" />
            </form>
        </div>
    </div>
  • 写回答

1条回答 默认 最新

  • drnysdnnb2909701 2017-01-21 09:57
    关注

    As php is server side scripting language php script runs only on server side ... Here you are trying to handle/run php script on client side which is not correct way.. to show data make following changes in your success function.

    $.ajax({
                    type: 'POST',
                    url: 'http://dev.nostools.cz/',
                    data: $('.form').serialize(),
                    ***success: function (data) {***
                      $("#show").html($("#console").val());
                    }
                  });
    
    
    <?php
        $_POST['console'];
        //process your data.
        echo $processed_data;
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?