0

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.

8
  • 1
    You have to url-encode POST data you send Commented Jun 19, 2014 at 9:02
  • You would have to show more of the javascript.. specifically where user is being initialised and given a value too Commented Jun 19, 2014 at 9:02
  • @hindmost give me an example--brain dead right now. Commented Jun 19, 2014 at 9:02
  • @Pricey give me one moment please. Commented Jun 19, 2014 at 9:03
  • 1
    Why you use pure JS? Use jQuery, it make to do AJAX requests more simpler Commented Jun 19, 2014 at 9:06

2 Answers 2

1

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");
Sign up to request clarification or add additional context in comments.

2 Comments

I now see what you mean now, I lacked my header... .-. Clumsy me.. By the way I hate you (Not really just jealous you solved it in probably minutes of looking while I been an hour debugging and stuff). But thanks a lot... Wanted to get this feature done before I went to sleep...
lol, no worries. I started with xmlhttp back in 2001, learned the hard way without any documentation. Literally took me weeks to figure out how to POST ;)
0

Have you tried to use :

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

I doubt it may be caused by the encoding.

5 Comments

He isn't setting the xmlhttp correctly to do a post, so $_POST won't find any variables because it's actually in the $_GET
@user3036342 He is setting the POST in xmlhttp.open("POST",...); right? You are saying this is wrong?
No it's not wrong, I edited my answer to reflect that he is also forgetting to set the content type to do a successful post, so even though he specifies POST, it will go through as a GET.
@user3036342 I am also thinking that he should add the content type, but it's not compulsory.
He was right.... I did a dummy test still didn't work... Now it works. It was the header.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.