AJAX变量没有从PHP文件中读取

AJAX variable are not reading from PHP file?

本文关键字:文件 读取 PHP 变量 AJAX      更新时间:2023-09-26

这是我的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发送正确的post。编辑你还缺少内容类型头来做一个成功的post

edit您还应该意识到使用您正在使用的技术可以发送的数量是有限制的。(这是一个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");

您是否尝试使用:

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

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