php 未收到 isset($_POST['value']) 数据

isset($_POST['value']) data is not recieved by php

我正在尝试将带有 POST 的 javascript 文件中的一些值发送到 php 页面。

AJAX代码:

let inputval = $input.val();
$.ajax({url: "../checkout/test.php", type : 'post', data : {'inputval': inputval}, success: 
function(data){ 
                console.log(data);
              }

PHP代码

 if (isset($_POST['inputval'])){
        $result = $_POST['inputval'];
        echo json_encode($result);
    }
    else {
        echo 'Not recieved';
    }

当我在网络中检查它时,一切都符合预期,但是当我打开实际页面时,却没有收到数据。

when I open the actual page the data is not recieved.

首先,您使用 JavaScript 发出 POST 请求。数据在网络选项卡中可见。它被发送到 PHP 程序。 PHP 程序响应。响应记录到控制台。

然后您“打开实际页面”。这是一个新请求。它不是带有数据的 POST 请求。 $_POST这次将是空的。响应将是对新请求的响应,而不是先前的 POST 请求。由于响应的内容取决于 $_POST 并且已经更改,因此此响应不同。

如果你想存储来自第一个请求的inputval数据然后在后续请求中显示它你需要显式 将其存储在某处(例如数据库或会话变量),然后在收到后续请求时检查该位置的数据。

逻辑会 运行 类似于:

IF this is a POST request
    store the inputval in the session
    send a confirmation message to the client
ELSE IF the session contains an inputval
    send it to the client
ELSE
    send a message to say that there is no data to the client