Skip to content

Instantly share code, notes, and snippets.

@javierguerrero
Created June 15, 2015 20:44
Show Gist options
  • Save javierguerrero/3d1a5e7ce3959692e041 to your computer and use it in GitHub Desktop.
Save javierguerrero/3d1a5e7ce3959692e041 to your computer and use it in GitHub Desktop.
<div id="hidden_form_container" style="display:none;"></div>
function postRefreshPage () {
var theForm, newInput1, newInput2;
// Start by creating a <form>
theForm = document.createElement('form');
theForm.action = 'somepage.php';
theForm.method = 'post';
// Next create the <input>s in the form and give them names and values
newInput1 = document.createElement('input');
newInput1.type = 'hidden';
newInput1.name = 'input_1';
newInput1.value = 'value 1';
newInput2 = document.createElement('input');
newInput2.type = 'hidden';
newInput2.name = 'input_2';
newInput2.value = 'value 2';
// Now put everything together...
theForm.appendChild(newInput1);
theForm.appendChild(newInput2);
// ...and it to the DOM...
document.getElementById('hidden_form_container').appendChild(theForm);
// ...and submit it
theForm.submit();
}
<!--This is equivalent to submitting this HTML form:-->
<form action="somepage.php" method="post">
<input type="hidden" name="input_1" value="value 1" />
<input type="hidden" name="input_2" value="value 2" />
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment