I do not understand where lose when passing JSON to the server, look for it together?

Sending it to the server line:
var msg = {},
str;
 msg.name = name.value;
 msg.mail = mail.value;
 msg.phone = phone.value;
 msg.select = select;
 msg.comment = comment;

 str = JSON.stringify(msg);

 //msg = serialize(document.querySelector('form'));

 var xhr = new XMLHttpRequest();

 // 2. Configure it: POST request URL 'mail.php'
 xhr.open('POST', 'mail.php', true);

 xhr.addEventListener('load', function() {
 if (xhr.status != 200) {
 // handle the error
 alert( xhr.status + ': '+ xhr.statusText ); // sample output: 404: Not Found
 } else {
 document.querySelector('.form__content').style.display = 'none';
 document.querySelector('.form-successful').style.display = 'block';
}
});

xhr.send(str);
 console.log(str);


Get decoder and sending mail:
$obj = json_decode($_POST, true);

 $to = "fio@mail.ru";
 $subject = "Application";
 $from = "info@giraffe-ra.ru";
 $headers = "From: $from";
mail($to,$subject,$obj[name],$headers);
 echo "Mail Sent.";


Any idea why the letter except the title is nothing?
July 12th 19 at 17:15
2 answers
July 12th 19 at 17:17
Solution
instead of $_POST use:
file_get_contents('php://input');

offtopic picture
0_c03f9_ada4c32a_orig.jpg
I apologize, that was interesting, I am also always on the side took the php $_POST would suggest, why is it impossible? - grayson_Bosco commented on July 12th 19 at 17:20
: $_POST is an array that is populated with data received from the user in the form "var=val&var2=val2", if parse fails, it leaves the array empty. - Jaylen.Les commented on July 12th 19 at 17:23
Well, if the post request is sent from the server to the server, it is also better to take as file_get_contents('php://input');? - grayson_Bosco commented on July 12th 19 at 17:26
Or is it just the AJAX request? - grayson_Bosco commented on July 12th 19 at 17:29
: what you do is perversion. You can't do neither when communication client-server or communication server-to-server (anything from the client-server interaction is no different). - parker commented on July 12th 19 at 17:32
Correctly formed POST request is handled automatically by php and you can work with superglobal array $_POST. But for this, of course, the request should normally generate. The link to how it is done, I gave you earlier. - parker commented on July 12th 19 at 17:35
about coding and the formation of the array data POST clear, but not clear the principles of choice $request = $_POST and $request = file_get_contents('php://input') - grayson_Bosco commented on July 12th 19 at 17:38
The principle is simple - if you invent the wheel and instead of the standard formatting of the body of a POST request that is automatically supported everything connected with the web, write in the body of the serialized json (using your custom format), then you need to use php://input. For everything else there's MasterCard. - parker commented on July 12th 19 at 17:41
July 12th 19 at 17:19
Solution
If you eat cactus perusopetusta bike, then do it right:
json_decode(file_get_contents('php://input'), true);


Well, if you want to do normally, then you here.
Thanks again, the essence of all exercise is to write on pure js, and as you can see most of the references to jQuery.
But Ilya Cantor read. - grayson_Bosco commented on July 12th 19 at 17:22
"To write on pure js" and "use standard have been invented mechanisms"is not mutually exclusive. - Jaylen.Les commented on July 12th 19 at 17:25

Find more questions by tags PHPJSON