Skip to content

Instantly share code, notes, and snippets.

@henriquegogo
Created November 30, 2024 14:22
Show Gist options
  • Save henriquegogo/2872fbcb7f4f2e9351b4031a2e0fc23f to your computer and use it in GitHub Desktop.
Save henriquegogo/2872fbcb7f4f2e9351b4031a2e0fc23f to your computer and use it in GitHub Desktop.
Form The Win!
<script>
function formRequest(method, action, body) {
event.preventDefault();
fetch(action, { method, body })
.then(res => res.redirected ? location.href = res.url : res.text())
.then(document.write.bind(document));
}
const PUT = form => formRequest("PUT", form.action, new FormjData(form));
const DELETE = form => formRequest("DELETE", form.action);
</script>
<form action="/">
<input type="text" name="title" />
<input type="submit" value="GET" />
</form>
<form action="/" method="POST">
<input type="text" name="title" />
<input type="submit" value="POST" />
</form>
<form action="/" onsubmit="PUT(this)">
<input type="text" name="title" />
<input type="submit" value="PUT" />
</form>
<form action="/id1" onsubmit="DELETE(this)">
<input type="text" name="title" />
<input type="submit" value="DELETE" />
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment