Matthew
Matthew

Reputation: 3234

AJAX variable are not reading from PHP file?

This is my javascript that holds the function to save the file.

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);
    }
  }
}

This is my files that is posted too.

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

?>

This is the output I receive on the php file.

savedMaps//.html

It should be more like this

savedMaps/randomName/fileName.html

EDIT:

To set for the user.

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

To set for the data... It is under the saveMap() function and starts with map.

Upvotes: 0

Views: 73

Answers (2)

user3036342
user3036342

Reputation: 1045

You are using PHP's $_POST get, you're not posting any variables, you should use $_GET in your situation, or change your xmlhttp send to post properly. edit you are also missing the content type header to do a successful post

edit You should also be aware that there is a limit on how much you can send through using the technique you're using. (which is a get, not a post, even though you specify it)

I'd also recommend looking into jQuery for cross-browser compatibility and ease of use.

edit

Here's some code that will allow you to pick it up via POST:

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

Upvotes: 1

PixelsTech
PixelsTech

Reputation: 3347

Have you tried to use :

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

I doubt it may be caused by the encoding.

Upvotes: 0

Related Questions