Created
November 30, 2024 14:22
-
-
Save henriquegogo/2872fbcb7f4f2e9351b4031a2e0fc23f to your computer and use it in GitHub Desktop.
Form The Win!
This file contains 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, 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