Skip to content

Instantly share code, notes, and snippets.

@mauriciosoares
Created February 24, 2016 21:57
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 mauriciosoares/c0e2271c95d0770304cf to your computer and use it in GitHub Desktop.
Save mauriciosoares/c0e2271c95d0770304cf to your computer and use it in GitHub Desktop.
/* @flow */
import getDocument from 'shared/utils/get-document';
export default function submitGoogleForm(fields, mapper, googleFormId='google-form') {
const document = getDocument();
const formData = Object.keys(mapper).reduce((data, key) => {
data[mapper[key]] = fields[key];
return data;
}, {});
const googleForm = document.getElementById(googleFormId);
if (googleForm === null) {
throw new Error('You must have an embedded iframe with your Google Form');
}
const form = document.createElement('form');
form.action = googleForm.src.replace('viewform', 'formResponse');
form.method = 'POST';
form.target = googleFormId;
form.style = 'display: none';
Object.keys(formData).forEach(key => {
let field = document.createElement('input');
field.name = key;
field.value = formData[key];
form.appendChild(field);
});
document.body.appendChild(form);
form.submit();
document.body.removeChild(form);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment