Skip to content

Instantly share code, notes, and snippets.

@eduardocardoso
Created January 7, 2016 12:52
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 eduardocardoso/0e7e61c173328db9d7e4 to your computer and use it in GitHub Desktop.
Save eduardocardoso/0e7e61c173328db9d7e4 to your computer and use it in GitHub Desktop.
Function to post data without having to create html elements beforehand
function postData(url, data) {
var theDiv = document.createElement('div');
theDiv.style.display = 'none';
var theForm = document.createElement('form');
theForm.action = url;
theForm.method = 'post';
for (var key in data) {
var newInput = document.createElement('input');
newInput.type = 'hidden';
newInput.name = key;
newInput.value = data[key];
theForm.appendChild(newInput);
}
theDiv.appendChild(theForm);
document.body.appendChild(theDiv);
theForm.submit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment