SIGN IT (ORGANIZATION)
var xhr = new XMLHttpRequest(); xhr.open('GET', 'https://5gspaceappeal.page/ajaxorganizationform'); xhr.onload = function () { if (xhr.status === 200) { var newhtmltext = xhr.responseText; document.getElementById("signatureform").innerHTML = newhtmltext; } else { alert('Fetching signature form failed. Returned status of ' + xhr.status); } }; xhr.send();
// these two functions set and remove the class is-invalid-input function set_invalid_input(elname) { var element, name, arr; element = document.getElementById(elname); name = "is-invalid-input"; arr = element.className.split(" "); if (arr.indexOf(name) == -1) { element.className += " " + name; } }
function remove_invalid_input(elname) { var element = document.getElementById(elname); element.className = element.className.replace(/\bis-invalid-input\b/g, ""); }
function validateEmail(email) { var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; return re.test(String(email).toLowerCase()); }
function removeElement(elementId) { // Removes an element from the document var element = document.getElementById(elementId); if (element) { element.parentNode.removeChild(element); } }
// the postposthandler does the validation of the form // and calls post_submission if it passes. function posthandler() {
// first clear the errors var errors = []; document.getElementById("errormessages").innerHTML = ""; removeElement("aspmsg"); removeElement("wppmsg"); removeElement("ctymsg"); removeElement("orgmsg"); removeElement("cidmsg"); removeElement("conmsg"); removeElement("csmsg"); removeElement("emlmsg");
sigform = document.getElementById("sigform"); for (var i = 0; i < sigform.elements.length; i++) { var element = sigform.elements[i]; element.className = element.className.replace(/\bis-invalid-input\b/g, ""); } organization = document.getElementById('organization').value; if (organization == '') { set_invalid_input('organization'); cty = document.getElementById('organization') cty.insertAdjacentHTML( 'afterend', '
' ); errors.push("Organization name is required."); }
city_state = document.getElementById('city_state').value; if (city_state == "") { //set_invalid_input('city_state'); //cty = document.getElementById('city_state') //cty.insertAdjacentHTML( //'afterend', //'
' //); //errors.push("City/State is required."); }
country_id = document.getElementById('country_id').value; if (country_id == "") { set_invalid_input('country_id'); cty = document.getElementById('country_id') cty.insertAdjacentHTML( 'afterend', '
' ); errors.push("Country is required."); }
contact = document.getElementById('contact').value; if (contact == "") { set_invalid_input('contact'); cty = document.getElementById('contact') cty.insertAdjacentHTML( 'afterend', '
' ); errors.push("Contact name is required."); }
contactposition = document.getElementById('contactposition').value; if (contactposition == "") { //set_invalid_input('contactposition'); //errors.push("Contact position is required."); }
email = document.getElementById('email').value; if (email == "" || !validateEmail(email)) { set_invalid_input('email'); cty = document.getElementById('email') cty.insertAdjacentHTML( 'afterend', '
' ); errors.push("Valid email is required."); }
appeal_submission_permission = document.getElementById('appeal_submission_permission').checked; if (!appeal_submission_permission) { set_invalid_input('appeal_submission_permission'); asp = document.getElementById('appeal_submission_permission') asp.insertAdjacentHTML( 'afterend', '
' ); errors.push("Appeal submission permission is required.");
}
web_publication_permission = document.getElementById('web_publication_permission').checked; if (!web_publication_permission) { set_invalid_input('web_publication_permission'); wpp = document.getElementById('web_publication_permission') wpp.insertAdjacentHTML( 'afterend', '
' ); errors.push("Web publication permission is required."); }
if (errors.length > 0) { document.getElementById("errormessages").innerHTML = "
") + "
"; return; }
post_submission();
}
function post_submission() { var xhr = new XMLHttpRequest(); sigform = document.getElementById("sigform"); formdata = serialize(sigform); //alert('http://signature.test/ajaxsignaturesave?' + formdata); xhr.open('POST', 'https://5gspaceappeal.page/ajaxorganizationsave?' + formdata); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.onload = function () { if (xhr.status === 200) { window.location.href = "https://5gspaceappeal.org/thank-you-post-signature"; //alert('Worked ok, next redirect to thankyou'); } else if (xhr.status !== 200) { alert('Submission failed. Returned status of ' + xhr.status); } }; // xhr.send(encodeURI('name=' + newName)); xhr.send(); }
/*! * Serialize all form data into a query string * (c) 2018 Chris Ferdinandi, MIT License, https://gomakethings.com * @param {Node} form The form to serialize * @return {String} The serialized form data */ var serialize = function (form) {
// Setup our serialized data var serialized = []; // Loop through each field in the form for (var i = 0; i < form.elements.length; i++) { var field = form.elements[i]; // Don't serialize fields without a name, submits, buttons, file and reset inputs, and disabled fields if (!field.name || field.disabled || field.type === 'file' || field.type === 'reset' || field.type === 'submit' || field.type === 'button') continue; // If a multi-select, get all selections if (field.type === 'select-multiple') { for (var n = 0; n < field.options.length; n++) { if (!field.options[n].selected) continue; serialized.push(encodeURIComponent(field.name) + "=" + encodeURIComponent(field.options[n].value)); } } // Convert field data to a query string else if ((field.type !== 'checkbox' && field.type !== 'radio') || field.checked) { serialized.push(encodeURIComponent(field.name) + "=" + encodeURIComponent(field.value)); } } return serialized.join('&'); };