Javascript AJAX变量未从PHP文件中读取?

Javascript AJAX变量未从PHP文件中读取?,javascript,php,ajax,Javascript,Php,Ajax,这是我的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.XMLHTT

这是我的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);
    }
  }
}
函数saveMap()
{
var-xmlhttp;
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
map=document.getElementById(“SecortTableMap”).innerHTML;
数据='';
数据+=地图;
数据+='';
document.getElementById(“SecortTableMap”).innerHTML=数据;
//警惕(“完成”);
//警报(数据);
如果(文件名==“lastSave-重命名”){
返回警报(“请在[CONFIG]下设置文件名”);
}
否则{
//警报(数据);
//警报(用户);
//警报(文件名);
open(“POST”http://pardustools.comuf.com/saveMap.php?t=“+Math.random(),true);
send('map='+data+'&user='+user+'&fileName='+fileName);
//警报(数据);
//警报(用户);
//警报(文件名);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
//返回警报(“文件已成功保存!”);
返回警报(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']);?>";
user=”“;
要为数据设置。。。
它位于saveMap()函数下,以map开头。

您是否尝试过使用:

xmlhttp.send('map=dummy&user='+user+'&fileName='+fileName);

我怀疑这可能是由编码引起的。

您使用的是PHP的$\u POST get,您没有发布任何变量,您应该在您的情况下使用$\u get,或者正确更改xmlhttp send to POST。编辑您还缺少内容类型标题,无法成功发布文章

编辑您还应该意识到,使用所使用的技术可以发送的信息量是有限制的。(这是一个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");

您必须对发送的数据进行url编码
POST
您必须显示更多的javascript。。特别是在初始化
用户
并给定值的情况下too@hindmost给我举个例子——现在脑死亡。@Pricey请给我一分钟。你为什么使用纯JS?使用jQuery,它可以简化AJAX请求。他没有正确设置xmlhttp来执行post,因此$\u post找不到任何变量,因为它实际上在$_GET@user3036342他正在xmlhttp.open中设置POST(“POST”,…);正确的?你是说这是错的吗?不,这不是错的,我编辑我的答案是为了反映他也忘了设置内容类型来完成一篇成功的文章,所以即使他指定了文章,它也会作为一个GET来完成。@user3036342我也认为他应该添加内容类型,但这不是强制性的。他是对的。。。。我做了一个模拟测试,但仍然不起作用。。。现在它起作用了。是头球。我现在明白你的意思了,我没有头球…-。笨手笨脚的我。。顺便说一句,我恨你(不是真的嫉妒你在我一个小时的调试和诸如此类的事情中用了几分钟的时间就解决了这个问题)。但是非常感谢。。。想在睡觉前完成这个功能…哈哈,不用担心。我早在2001年就开始使用xmlhttp,在没有任何文档的情况下艰难地学习。我花了好几个星期才弄明白如何发布;)