Reputation: 6277
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();
Upvotes: 1
Views: 50
Reputation: 87203
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
Upvotes: 3
Reputation: 1379
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);
Upvotes: 2