Skip to content

Instantly share code, notes, and snippets.

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 eduardoluizgs/0a22ec55e06d1a88dd18a88896b0ba51 to your computer and use it in GitHub Desktop.
Save eduardoluizgs/0a22ec55e06d1a88dd18a88896b0ba51 to your computer and use it in GitHub Desktop.
JavaScript Post Form
function post_to_url(path, params, method) {
method = method || "post";
var form = document.createElement("form");
//Move the submit function to another variable
//so that it doesn't get overwritten.
form._submit_function_ = form.submit;
form.setAttribute("method", method);
form.setAttribute("action", path);
for(var key in params) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", params[key]);
form.appendChild(hiddenField);
}
document.body.appendChild(form);
form._submit_function_(); //Call the renamed function.
}
// Works!
post_to_url("http://url", {"key": "value"} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment