Php 使用jQueryAjax将数据发送到另一个无法工作的页面

Php 使用jQueryAjax将数据发送到另一个无法工作的页面,php,jquery,ajax,Php,Jquery,Ajax,我使用ajax将一些数据从test.php发送到另一个页面order.php,但当我导航到order.php并尝试回显数据时,会收到“Undefined index…”错误消息 我已经在text.php文件中完成了console.log,并确认数据值已成功设置。但它不会反映在order.php文件中。我还在这两个文件中启动了会话 注意:在将ajax数据发送到DB之前,我需要通过order.php中的$_POST['data']获取数据,并保存在一个变量中,以便继续在DB中插入值。在这种情况下,我

我使用ajax将一些数据从test.php发送到另一个页面order.php,但当我导航到order.php并尝试回显数据时,会收到“Undefined index…”错误消息

我已经在text.php文件中完成了console.log,并确认数据值已成功设置。但它不会反映在order.php文件中。我还在这两个文件中启动了会话

注意:在将ajax数据发送到DB之前,我需要通过order.php中的$_POST['data']获取数据,并保存在一个变量中,以便继续在DB中插入值。在这种情况下,我甚至无法获得数据。出于某种原因,发布到order.php的数据没有到达该文件。这就是我想解决的问题。如何将我的ajax post数据获取到order.php,以便将其用于其他用途

请问我的代码有什么问题?见下面的代码:

test.php

<script type="text/javascript">
            $(document).ready(function(){
                $(".addItemBtn").click(function(event){
                    var $form = $(this).closest(".formclass");
                    var qty = $form.find(".naira-input").val(); //form input`
                    var total = $('.amount').html(); //from javascript innerhtml

                    
                $.ajax({
                     url:'order.php',
                     method:'post',
                     data:{grandTotal:total, nairaRate:qty}, // dont worry about this. The values are succesfully set in console.log
                     success:function(response)
                     {
                          console.log(response+' values were sent successfully');// I tested and confirmed that data was sent successfully
                    }
                     
                });
            });
        });


        </script>
    <?php
    session_start();
    
 $_SESSION['grandTotal']= $_POST['grandTotal'];
      $_SESSION['nairaRate']=$_POST['nairaRate'];

    echo $_SESSION['grandTotal']; // I get Undefined index error here
    echo  $_SESSION['nairaRate']; // I get Undefined index error here
    
    ?>

$(文档).ready(函数(){
$(“.addItemBtn”)。单击(函数(事件){
var$form=$(this).closest(“.formclass”);
变量数量=$form.find(“.naira input”).val();//表单输入`
var total=$('.amount').html();//来自javascript innerhtml
$.ajax({
url:'order.php',
方法:'post',
数据:{grandTotal:total,nairaRate:qty},//不用担心。这些值已在console.log中成功设置
成功:功能(响应)
{
log(response+'值已成功发送');//我测试并确认数据已成功发送
}
});
});
});
order.php

<script type="text/javascript">
            $(document).ready(function(){
                $(".addItemBtn").click(function(event){
                    var $form = $(this).closest(".formclass");
                    var qty = $form.find(".naira-input").val(); //form input`
                    var total = $('.amount').html(); //from javascript innerhtml

                    
                $.ajax({
                     url:'order.php',
                     method:'post',
                     data:{grandTotal:total, nairaRate:qty}, // dont worry about this. The values are succesfully set in console.log
                     success:function(response)
                     {
                          console.log(response+' values were sent successfully');// I tested and confirmed that data was sent successfully
                    }
                     
                });
            });
        });


        </script>
    <?php
    session_start();
    
 $_SESSION['grandTotal']= $_POST['grandTotal'];
      $_SESSION['nairaRate']=$_POST['nairaRate'];

    echo $_SESSION['grandTotal']; // I get Undefined index error here
    echo  $_SESSION['nairaRate']; // I get Undefined index error here
    
    ?>

当您导航到order.php时,为会话分配值的代码再次运行,这是您没有发出post请求的唯一时间,因此不会设置值


不要使用与设置数据相同的端点来显示数据。

使用AJAX时,其目的是不必导航到其他地方。将您的数据保存到order.php和
echo{“success”:“您的订单已收到,订单号为#”。“$orderNumber.”)例如。然后可以向用户显示response.success使用纯Javascript(fetch或XMLHttpRequest)将数据发布到order.php,而无需刷新页面。您不需要JQuery。@mplungjan:您是说我不能在另一个页面中使用Ajax发布的数据,除非我先将其保存在DB中,然后在下一个页面中手动访问它?是的,除非您也将其保存在local/sessionStorage中,但是为什么不从数据库中获取数据,以便ppl可以链接到另一个页面呢too@AbsoluteBeginner:我需要导航到order.php onclick事件并使用order.php中发布的Ajax数据