a <form>
is an HTTP element that contains input elements
<form method='post'>
Name: <input type='text' name='name' value='Alice'>
<br>
Password: <input type='password' name='password'>
<br>
<input type='submit'>
</form>
<form>
element will correspond to a block element (viz. the border of the form)
<form>
is an inline elementdiv
form
alone<form method='get' action='/login'>
method
corresponds to the first word in the HTTP protocol
action
is the server path to submit the form to
GET
means "return me a page (based on these parameters)"POST
means "take these parameters (and return me a page)"Basically, GET
is for reading and POST
is for writing
but that distinction is often blurry
Also,
GET
sends all parameters via the request URL
POST
sends some or all parameters via the request body* <form method='GET' action='/search'>
<label>Search by Author: <input type="search" name="author"></label>
<input type='submit' value='Search'>
</form>
<label>
marks some text as belonging to a certain input element<input name='q' type='search'>
is a text field
<input type='submit' value='Search'>
is a button whose label is the string "Search"
There are many more types of form elements (or "widgets") that let the user enter data in a wide variety of formats.
<form href='#'>
in your HTMLevent.preventDefault();
in your JS event handlerform.submit()
on the form DOM elementq=apple&submit=Search
After the server receives and processes the request, it can return any HTTP response.
Usually that request is a full HTML page (for display to the user) or a JSON document (for use by your client-side app).
Sometimes the response is a 302 redirect which helps keep your endpoints and address bar clean:
...but many times it's an all-in-one handler which redraws the original page.
Sometimes the response is an error.
It's important for you to design your app to be robust, meaning that if an error happens, it will fail gracefully.
Usually this means informing the user that the request could not be completed, and giving them the option to try again immediately, or asking them to try again later.
In the Fetch API, you can provide an catch
callback which will receive network errors if and when they happen; you can also check if (!response.ok) {
inside your main then
callback.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form - docs https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms - guide
/