javascriptphpajaxgoogle-maps

How to use XMLHTTPRequest with POST and params properly? PHP not getting POST info


I'm trying to build a Google Maps web app. I store flats and want to edit them. I provide a code which users insert and get the info from their previously stored flat. I'm using PHP an JS.

This is what I'm trying to do:

In the client side I use:

var clave="<?=$_POST['identificador']?>";
    var params="clave="+clave;

function downloadUrl(params,url, callback) {
  var request = window.ActiveXObject ?
      new ActiveXObject('Microsoft.XMLHTTP') :
      new XMLHttpRequest; //en cierto modo es una API, acepta requests HTTP.

  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      request.onreadystatechange = doNothing;
      callback(request.responseText, request.status);
    }
  };
    
  request.open("POST", url, true);
  request.send(params);
}

I get "identificador" from a previous PHP page, it is getting the right value.

Then I call the downloadURL function :

downloadUrl(params,"phpsqlajax_genxml1.php", function(data) {
    var xml = parseXml(data); 
    var markers = xml.documentElement.getElementsByTagName("marker"); //coge todos los markers
    //GETS XML RESPONSE AND PUTS MARKERS INTO MAPS
    }
  });

But when I call phpsqlajax_genxml1.php it doesn't get the POST value:

$clave = $_POST["clave"];
echo $clave; //this is not echoing nothing
$query = "SELECT * FROM markers WHERE clave = '{$clave}'";

Is there any better way to do this or what am I doing it wrong?


Solution

  • As Rob W answered, you can solve this by adding some headers:

     request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");request.setRequestHeader("Content-Length", params.length);