Here we send all form fields as json to backend on form submit.
JavaScript
$("#form").submit(function(e) {
e.preventDefault()
var xhr = new XMLHttpRequest();
var params = $('#form').serialize();
xhr.open('POST', '/mail.php', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if(xhr.status == 200) {
$('#form').css("display", "none");
}
}
};
xhr.send(params);
});