Created
March 3, 2021 13:43
-
-
Save RyadPasha/3d1087a7040482c4aee4be110b1ab76d to your computer and use it in GitHub Desktop.
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
/** | |
* sends a request to the specified url from a form. this will change the window location. | |
* @param {string} path the path to send the post request to | |
* @param {object} params the paramiters to add to the url | |
* @param {string} [method=post] the method to use on the form | |
*/ | |
function post(path, params, method='POST') { | |
// The rest of this code assumes you are not using a library. | |
// It can be made less wordy if you use one. | |
const form = document.createElement('form'); | |
form.method = method; | |
form.action = path; | |
for (const key in params) { | |
if (params.hasOwnProperty(key)) { | |
const hiddenField = document.createElement('input'); | |
hiddenField.type = 'hidden'; | |
hiddenField.name = key; | |
hiddenField.value = params[key]; | |
form.appendChild(hiddenField); | |
} | |
} | |
document.body.appendChild(form); | |
form.submit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment