diff --git a/lib/commonjs/toolbox/utils/google-forms/submit-google-form.js b/lib/commonjs/toolbox/utils/google-forms/submit-google-form.js new file mode 100644 index 00000000..210b24eb --- /dev/null +++ b/lib/commonjs/toolbox/utils/google-forms/submit-google-form.js @@ -0,0 +1,24 @@ +function submitGoogleForm(form, callback) { + form.addEventListener('submit', function (e) { + var mappedInput = []; + Array.from(form).map(function (input) { + if (input.name && input.value !== undefined) { + mappedInput.push(input.name + "=" + input.value); + } + return mappedInput; + }); + var data = mappedInput.join('&'); + var xhr = new XMLHttpRequest(); + xhr.open('POST', form.action, true); + xhr.setRequestHeader('Accept', 'application/xml, text/xml, */*; q=0.01'); + xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=UTF-8'); + xhr.send(data); + xhr.onreadystatechange = function () { + if (xhr.readyState == 4) { + callback(); + } + }; + e.preventDefault(); + }); +} +//# sourceMappingURL=submit-google-form.js.map \ No newline at end of file diff --git a/lib/es6/toolbox/utils/google-forms/submit-google-form.js b/lib/es6/toolbox/utils/google-forms/submit-google-form.js new file mode 100644 index 00000000..210b24eb --- /dev/null +++ b/lib/es6/toolbox/utils/google-forms/submit-google-form.js @@ -0,0 +1,24 @@ +function submitGoogleForm(form, callback) { + form.addEventListener('submit', function (e) { + var mappedInput = []; + Array.from(form).map(function (input) { + if (input.name && input.value !== undefined) { + mappedInput.push(input.name + "=" + input.value); + } + return mappedInput; + }); + var data = mappedInput.join('&'); + var xhr = new XMLHttpRequest(); + xhr.open('POST', form.action, true); + xhr.setRequestHeader('Accept', 'application/xml, text/xml, */*; q=0.01'); + xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=UTF-8'); + xhr.send(data); + xhr.onreadystatechange = function () { + if (xhr.readyState == 4) { + callback(); + } + }; + e.preventDefault(); + }); +} +//# sourceMappingURL=submit-google-form.js.map \ No newline at end of file diff --git a/src/toolbox/utils/google-forms/submit-google-form.ts b/src/toolbox/utils/google-forms/submit-google-form.ts new file mode 100644 index 00000000..18b92326 --- /dev/null +++ b/src/toolbox/utils/google-forms/submit-google-form.ts @@ -0,0 +1,35 @@ +/** + * Submits HTML form data to a google form. + * @param form The HTML form to be submitted. + * @param callbacks Optional callback function to be run on form submission. + */ + +function submitGoogleForm(form: HTMLFormElement, callback?: () => {}): void { + form.addEventListener('submit', e => { + const mappedInput: string[] = []; + Array.from(form).map((input: HTMLInputElement) => { + if (input.name && input.value !== undefined) { + mappedInput.push(`${input.name}=${input.value}`); + } + return mappedInput; + }); + const data = mappedInput.join('&'); + + const xhr = new XMLHttpRequest(); + xhr.open('POST', form.action, true); + xhr.setRequestHeader('Accept', 'application/xml, text/xml, */*; q=0.01'); + xhr.setRequestHeader( + 'Content-type', + 'application/x-www-form-urlencoded; charset=UTF-8' + ); + xhr.send(data); + + xhr.onreadystatechange = () => { + if (xhr.readyState == 4) { + callback(); + } + }; + + e.preventDefault(); + }); +}