我想将数据从一个页面传递到另一个页面。我尝试了下面的代码。 result_display.php总是给我一个空的_POST数组。如果有人能指出我哪里错了,我将不胜感激。谢谢。在PHP中使用AJAX发布数据
的index.php
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>js save session</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<input type="submit" id="mybutton" value="My Button">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#mybutton").click(function() {
$.ajax({
method: "POST",
url: "./result_display.php",
data: {name: "John", location: "Boston"}
})
.done(function (msg) {
console.log("Data Saved: " + msg);
});
window.location.href = "result_display.php";
});
});
</script>
</body>
</html>
result_display.php
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
?>
<?php
var_dump($_POST);
?>
尝试在这里的答案之一加入了“传统”的参数,以你的Ajax调用,如:https://stackoverflow.com/questions/5046930/jquery-send-string-as-post-parameters – agileMike
谢谢回复。它也一样。我首先运行index.php。然后刷新result_display.php,仍然有一个空的_POST数组。 –
啊,我误解了。其他意见是正确的。如果你做了一个POST然后一个GET,那么这两个请求是分开的。 GET不会有数据。如果在GET中需要这些参数,那么将它们作为查询字符串参数发送:window.location.href =“result_display.php?name = John&location = Boston”;然后你可能根本不需要POST。 – agileMike