function sendRequest(data, callBack = null, args = null) { var http = new XMLHttpRequest(); var url = window.location.origin + '/wp-admin/admin-ajax.php'; http.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { if (callBack) { window[callBack](this.responseText, args); } } }; http.open("POST", url, true); http.timeout = 40000; data = prepareHttpRequestData('MANABOOM_PUBLIC_REQUEST', data); http.send(data); } function handleResponse(response, args = null) { // response is a JSON {'status', 'result'} response = JSON.parse(response); let msgId = 'mana-msg' if (args != null && args.msgId) { msgId = args.msgId; } let msg = document.getElementById(msgId); let msgBox = msg.querySelectorAll('span')[0]; msg.classList.add("mana-hide"); if (response['status'] == 'OK') { msgBox.innerHTML = " " + response['result']; msg.classList.remove("mana-hide"); msg.classList.remove('error'); msg.classList.add('updated'); } else if (response['status'] == 'ERROR') { msgBox.innerHTML = " " + response['result']; msg.classList.remove("mana-hide"); msg.classList.remove('updated'); msg.classList.add('error'); } else { msgBox.innerHTML = "Invalid response."; msg.classList.remove("mana-hide"); } } function prepareHttpRequestData(action, data) { let formData = new FormData(); formData.append('action', action); for (key in data) { formData.append(key, data[key]); } return formData; } function sendForm(formId) { let formToSend = document.getElementById(formId); let data = {}; let msg = document.getElementById('mana-msg'); let msgBox = document.getElementById('mana-msg-value'); msgBox.innerHTML = "Connecting..."; msg.classList.remove("mana-hide"); Array.prototype.forEach.call(formToSend.elements, function(element) { data[element.name] = element.value; }); sendRequest(data, 'handleResponse'); } function checkDomain(isSubdomain, domain) { sendRequest(); }