使用ajax发送数据后如何将它们存储在PHP变量中?我创建了一个转储文件,我可以在其中看到该变量已发送,但是当我回显它们时却看不到它们?我怎么能看到他们?我通过 URL 发送获取数据并通过XMLHttpRequest(); 数据发布数据返回很好但是为什么它不存储在 PHP 变量中?
<?php
//dumping code to see received data
$output = "Post Variables\n";
$output .= print_r($_POST, true);
$output .= "\nGet Variables\n";
$output .= print_r($_GET, true);
$output .= "\nBody Content\n";
$output .= print_r(file_get_contents('php://input') ?: "empty", true);
file_put_contents("dump.txt", $output);
// End
if(isset($_GET['a'])) {
die('This is post data: ' . htmlspecialchars($_GET['a']));
}
if(isset($_POST['b'])) {
die('This is post data: ' . htmlspecialchars($_POST['b']));
}
echo "This is get variable: " .$a;
echo "This is post variable: " .$b;
?>
<html>
<head>
<script>
//sending ajax request to change table name on onclick event
function clickMe(j){
// Create our XMLHttpRequest object
var req = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
var dayName = document.getElementById("btn"+j).value;
var SVAR = "b="+dayName;
var url = "tempo.php?a="+dayName;
req.open("POST", url, true);
// Set content type header information for sending url encoded variables in the request
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
req.onreadystatechange = function() {
if(req.readyState == 4 && req.status == 200) {
let data_return = req.responseText;
document.getElementById("status1").innerHTML = data_return;
}
}
// Send the data to PHP now... and wait for response to update the status div
req.send(SVAR);
}
</script>
</head>
<body>
<h2>Ajax Post to PHP and Get Return Data</h2>
<button id="btn1" value="saturday" onclick="clickMe(1)">btn1</button>
<button id="btn2" value="sunday" onclick="clickMe(2)">btn2</button>
<br><br>
<div id="status1"></div>
</body>
</html>
红糖糍粑
30秒到达战场