0

I'm trying to send form data via XMLHttpRequest.send() and, on the server, $_POST is empty. What is the correct data syntax and content-type to use so that $_POST receives the data ?

Here's how I construct the data:

var data = JSON.stringify ({
  "first-name": document.getElementById("contact-first-name").value,
  "last-name": document.getElementById("contact-last-name").value,
  ...
});

And,

console.log ("data: " + data);

shows

data: {"first-name":"peri","last-name":"hartman", ...}

My sender function body is:

var url = "php/update_member.php";
var http = new XMLHttpRequest ();
http.onload = function ()
{
  console.log ("getMemberAsync results: status + http.status);
  console.log (http.responseText);
}
http.open ("POST", url, true);
http.setRequestHeader ("Content-Type", "application/json; charset=utf-8");
http.send (data);

On the server side, I have this early on in PHP:

print '$_POST: '; print_r ($_POST);

When the response comes back, the status is 200 and the responseText is simply

$_POST: Array
(
)

NOTE: question clarified at top and reopened.

Peri Hartman
  • 18,754
  • 17
  • 53
  • 91

0 Answers0