duanpo2813 2016-07-08 12:45
浏览 294
已采纳

使用AJAX时显示警告[重复]

This question already has an answer here:

I am two files, a PHP file and another file made of javascript and HTML.

PHP file :

<?php
session_start(); //start session

$_SESSION['data'] = "The content should be displayed as alert in the JS file";

JS/HTML File:

<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<form class="form-item" id="idForm">
        <input type="hidden" name="product_code" value="prince"/>
        <button id="btn_add" type="submit">Add to Cart</button>
</form>
<script>


    $(function(){
        $("#idForm").submit(function(e) {

            var url = "test2.php";

            $.ajax({
                type: "POST",
                url: url,
                data: $("#idForm").serialize(),
                success: function()
                {
                    alert(/*The content of the session in the php file should be display here*/); // show response from the php script.
                }
            });

            e.preventDefault();
        });
    });
</script>

In the PHP file, I have a session $_SESSION['data']. All I want to do, it is to be able display the content of that session in alert() after making the ajax request to test2.php

</div>
  • 写回答

2条回答 默认 最新

  • dongquanyu5816 2016-07-08 12:47
    关注

    Add response as parameter to success. Response is basically anything that would normally display in the browser (including syntax errors)

      $.ajax({
                type: "POST",
                url: url,
                data: $("#idForm").serialize(),
                success: function(resp)
                {
                    alert(resp); // show response from the php script.
                }
            });
    

    Make sure to echo what you wish to display in your PHP script. Anything which gets echoed will be returned in the AJAX response:

    <?php
    session_start(); //start session
    
    $_SESSION['data'] = "The content should be displayed as alert in the JS file";
    echo $_SESSION['data'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记