Created
January 7, 2016 12:52
-
-
Save eduardocardoso/0e7e61c173328db9d7e4 to your computer and use it in GitHub Desktop.
Function to post data without having to create html elements beforehand
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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