<分区>
我想将一个 JavaScript 变量 Data
发送到另一个 php 文件以用于 php 函数。 I realized we are unable to use PHP Sessions for that.我尝试使用 AJAX(使用 jQuery)来完成它,但我无法将 AJAX 请求初始化为 php 变量。但是 AJAX 给我一个正确的响应:(意味着 ajax 正在工作)。但是当我打印请求 [print_r($_POST)] 时,结果为零。
这里是主要的 html , javascript 部分。
<div id ='postData'>"+ PostDataResponse + "</div> // PostDataResponse is also ajax response, it is working correctly.
<a href='server.php' onclick= saveData()> click_Here_Link </a>
当我单击 click_Here_Link 链接时,我不会将数据作为 session 变量发送到 requestFile.php 文件。我像这样在客户端开发了 ajax post 方法。(它给了我正确的响应。)
function saveData(){
// $.post('/ server.php', { postingData: name});
var name = document.getElementById("postData").innerHTML.trim();
$.ajax ({
type: 'POST',
url: 'server.php',
dataType: 'text',
data: {postingData: name},
success: function (Response) {
alert(Response);
},
error: function () {
alert('failure...! ');
}
});
}
这是 server.php 代码。
<?Php
$boat = $_POST['postingData'];
echo $boat;
print_r($_POST);
?>
当打开 server.php 文件时出现错误。
Undefined index: postingData