Introduction to HTML Forms Flashcards
what does the Form element do
Just like a physical form, an HTML <form> element is responsible for collecting information to send somewhere else. Every time we browse the internet we come into contact with many forms and we might not even realize it. There’s a good chance that if you’re typing into a text field or providing an input, the field that you’re typing into is part of a <form>!
how a from works
How a Form Works
We can think of the internet as a network of computers which send and receive information. Computers need an HTTP request to know how to communicate. The HTTP request instructs the receiving computer how to handle the incoming information. More information can be found in our article about HTTP requests.
The <form> element is a great tool for collecting information, but then we need to send that information somewhere else for processing. We need to supply the <form> element with both the location of where the <form>‘s information goes and what HTTP request to make. Take a look at the sample <form> below:
<form>
</form>
In the above example, we’ve created the skeleton for a <form> that will send information to example.html as a POST request:
The action attribute determines where the information is sent. The method attribute is assigned a HTTP verb that is included in the HTTP request.
Note: HTTP verbs like POST do not need to be capitalized for the request to work, but it’s done so out of convention. In the example above we could have written method=”post” and it would still work.
The <form> element can also contain child elements. For instance, it would be helpful to provide a header so that users know what this <form> is about. We could also add a paragraph to provide even more detail. Let’s see an example of this in code:
<form>
<h1>Creating a form</h1>
<p>Looks like you want to learn how to create an HTML form. Well, the best way to learn is to play around with it.</p>
</form>
The example above doesn’t collect any user input, but we’ll do that in the next exercise. For now, let’s practice making the foundation of an HTML <form>!
what is the <form> used for
The <form> element is a great tool for collecting information
<form> </form>
what does the action and method attribute do
The action attribute determines where the information is sent.
The method attribute is assigned a HTTP verb that is included in the HTTP request.
how to add information to a from using <h1> and <p>
<form>
<h1>Creating a form</h1>
<p>Looks like you want to learn how to create an HTML form. Well, the best way to learn is to play around with it.</p>
</form>
how to add text input into a form using <input></input>
The first value for the type attribute we’re going to explore is “text”. When we create an <input></input> element with type=”text”, it renders a text field that users can type into
<form>
<input></input>
</form>
what is the type=”text” for in a input field
type=”text”, it renders a text field that users can type into. Note that the default value of type is “text”. It’s also important that we include a name attribute for the <input></input> — without the name attribute, information in the <input></input> won’t be sent when the <form> is submitted.
what is the value=”” for in input field
After users type into the <input></input> element, the value of the value attribute becomes what is typed into the text field. The value of the value
what is the name attribute for input field
The value of the value attribute is paired with the value of the name attribute and sent as text when the form is submitted. For instance, if a user typed in “important details” in the text field created by our <input></input> element:
When the form is submitted, the text: “first-text-field=important details” is sent to /example.html because the value of the name attribute is “first-text-field” and the value of value is “important details”.
why do we need a lable element for a form
For a user to properly identify an <input></input> we use the appropriately named <label> element.</label>
how do add a lable
The <label> element has an opening and closing tag and displays text that is written between the opening and closing tags. To associate a <label> and an <input></input>, the <input></input> needs an id attribute. We then assign the for attribute of the <label> element with the value of the id attribute of <input></input>, like so:</label></label></label>
<form>
<label>What do you want to eat?</label>
<br></br>
<input></input>
</form>
how to add the password input using <input></input>
An <input></input> element will replace input text with another character like an asterisk () or a dot (). The code below provides an example of how to create a password field:
<form>
<label>Password: </label>
<input></input>
</form>
when the password is submitted how will the data be shown
Even though the password field obscures the text of the password, when the form is submitted, the value of the text is sent. In other words, if “hunter2” is typed into the password field, “user-password=hunter2” is sent along with the other information on the form.
explain using adding numbes into a for musing type=’number’
setting type=”number” for an <input></input> we can restrict what users type into the input field to just numbers (and a few special characters like -, +, and .). We can also provide a step attribute which creates arrows inside the input field to increase or decrease by the value of the step attribute. Below is the code needed to render an input field for numbers:
<form>
<label> Years of experience: </label>
<input></input>
</form>
how to set up number using type= number step = 1
setting type=”number” for an <input></input> we can restrict what users type into the input field to just numbers (and a few special characters like -, +, and .). We can also provide a step attribute which creates arrows inside the input field to increase or decrease by the value of the step attribute. Below is the code needed to render an input field for numbers:
<form>
<label> Years of experience: </label>
<input></input>
</form>