Last active
April 4, 2025 02:28
-
-
Save henriquegogo/2872fbcb7f4f2e9351b4031a2e0fc23f to your computer and use it in GitHub Desktop.
Form The Win!
This file contains hidden or 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
<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