1

Retrieving the data with PHP, I cannot use $_POST; but $_GET. Why? Am I sending my form data incorrectly?

I'd have thought request.open("POST" would process the form as a POST and not GET? How may I sent it as a POST?

var request = new XMLHttpRequest();
request.open("POST","email.php?text=" + textarea.value + "&email=" + email.value, true);

request.onload = function() {
    if (request.status >= 200 && request.status < 400) {
        var resp = request.responseText;

        console.log(resp);

    }
};

request.send();
ditto
  • 5,917
  • 10
  • 51
  • 88

2 Answers2

3

Because you're adding data inside the URL.

Change your request to:

request.open("POST","email.php", true);
request.setRequestHeader("Content-length", 2); // 2 here is the no. of params to send
....


request.send("text=" + textarea.value + "&email=" + email.value);

Docs: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

Tushar
  • 85,780
  • 21
  • 159
  • 179
2

The reason is you are sending the variables in the url that is why you are getting in get. See this example post

var http = new XMLHttpRequest();
var url = "get_data.php";
var params = "lorem=ipsum&name=binny"; // all prams variable here
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
    }
}
http.send(params);
Faiz Rasool
  • 1,379
  • 1
  • 12
  • 20
  • http://stackoverflow.com/questions/2623963/webkit-refused-to-set-unsafe-header-content-length says there is no need to set a content length or connection. – ditto Jun 12 '15 at 12:27