我正在尝试使用 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';
}
当我在网络中检查时,一切都符合预期,但是当我打开实际页面时,未收到数据。
当我打开实际页面时,未收到数据。
首先使用 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