gpt4 book ai didi

javascript - AJAX 变量没有从 PHP 文件读取?

转载 作者:行者123 更新时间:2023-12-03 12:24:10 25 4
gpt4 key购买 nike

这是我的 JavaScript,它包含保存文件的功能。

function saveMap()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
map = document.getElementById("sectorTableMap").innerHTML;
data = '<table id="sectorTableMap">';
data += map;
data += '</table>';
document.getElementById("sectorTableMap").innerHTML = data;
//alert("done");
//alert(data);


if(fileName=="lastSave - RENAME") {
return alert("Please set a file name under [CONFIG]");
}
else {
//alert(data);
//alert(user);
//alert(fileName);
xmlhttp.open("POST","http://pardustools.comuf.com/saveMap.php?t="+Math.random(),true);
xmlhttp.send('map='+data+'&user='+user+'&fileName='+fileName);
//alert(data);
//alert(user);
//alert(fileName);
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//return alert("File has successfully been saved!");
return alert(xmlhttp.responseText);
}
}
}

这也是我发布的文件。

<?php
$user = strtolower($_POST['user']);
$map = $_POST['map'];
$fileName = "savedMaps/".$user."/".$_POST['fileName'].".html";
file_put_contents($fileName,$map);
echo $fileName."<br />".$map;

?>

这是我在 php 文件上收到的输出。

savedMaps//.html

应该是这样的

savedMaps/randomName/fileName.html

编辑:

为用户设置。

user = "<?php $cookie = $_COOKIE['mapperlogauth']; echo strtolower($cookie['user']);?>";

要设置数据...它位于saveMap()函数下,以map开头。

最佳答案

您正在使用 PHP 的 $_POST get,您没有发布任何变量,您应该在您的情况下使用 $_GET,或者更改您的 xmlhttp send 以正确发布。 编辑您还缺少成功发布内容的内容类型标题

编辑 您还应该意识到,使用您正在使用的技术可以发送的数据量是有限的。 (这是一个 get,而不是 post,即使您指定了它)

我还建议研究 jQuery 的跨浏览器兼容性和易用性。

编辑

下面是一些代码,可让您通过 POST 获取它:

xmlhttp.open("POST","ajax_test.asp",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fname=Henry&lname=Ford");

关于javascript - AJAX 变量没有从 PHP 文件读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24302665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com