Skip to content

Instantly share code, notes, and snippets.

@henriquegogo
Last active April 4, 2025 02:28
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) {
const action = event.target.action;
event.preventDefault();
fetch(action, { method, body: new FormData(event.target) })
.then(res => res.redirected && (location.href = res.url));
}
const PUT = formRequest.bind(null, "PUT");
const DELETE = formRequest.bind(null, "DELETE");
</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()">
<input type="text" name="title" />
<input type="submit" value="PUT" />
</form>
<form action="/id1" onsubmit="DELETE()">
<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