Skip to content

Instantly share code, notes, and snippets.

@iMagesh
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iMagesh/06b6ad7a41b308c3d995 to your computer and use it in GitHub Desktop.
Save iMagesh/06b6ad7a41b308c3d995 to your computer and use it in GitHub Desktop.
var pageUrl = "http://something.com/gsm";
var interactionId = "email-form"; //id of the product
document.getElementsByClassName(interactionId).onsubmit = function AfterFunction(e){
var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 ) {
if(xmlhttp.status == 200){
alert(JSON.stringify(customform(document.getElementsByClassName(interactionId))));
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
}
else if(xmlhttp.status == 400) {
alert('There was an error 400')
}
else {
alert('something else other than 200 was returned')
}
}
}
//alert(JSON.stringify(customform(document.getElementById(interactionId))));
}
function customform(form) {
var i, j, len, jLen, formElement, q = [];
function addNameValue(name, value) {
q.push(encodeURI(name) + '=' + encodeURI(value));
}
if (!form || !form.nodeName || form.nodeName.toLowerCase() !== 'form') {
throw 'You must supply a form element';
}
for (i = 0, len = form.elements.length; i < len; i++) {
formElement = form.elements[i];
if (formElement.name === '' || formElement.disabled) {
continue;
}
switch (formElement.nodeName.toLowerCase()) {
case 'input':
switch (formElement.type) {
case 'text':
case 'hidden':
case 'password':
case 'button': // Not submitted when submitting form manually, though jQuery does serialize this and it can be an HTML4 successful control
case 'submit':
addNameValue(formElement.name, formElement.value);
break;
case 'checkbox':
case 'radio':
if (formElement.checked) {
addNameValue(formElement.name, formElement.value);
}
break;
case 'file':
// addNameValue(formElement.name, formElement.value); // Will work and part of HTML4 "successful controls", but not used in jQuery
break;
case 'reset':
break;
}
break;
case 'textarea':
addNameValue(formElement.name, formElement.value);
break;
case 'select':
switch (formElement.type) {
case 'select-one':
addNameValue(formElement.name, formElement.value);
break;
case 'select-multiple':
for (j = 0, jLen = formElement.options.length; j < jLen; j++) {
if (formElement.options[j].selected) {
addNameValue(formElement.name, formElement.options[j].value);
}
}
break;
}
break;
case 'button': // jQuery does not submit these, though it is an HTML4 successful control
switch (formElement.type) {
case 'reset':
case 'submit':
case 'button':
addNameValue(formElement.name, formElement.value);
break;
}
break;
}
}
return q.join('&');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment