HTML: forms and data input

Lesson 4 of 17 · HTML, CSS and JavaScript

A login, a search, a survey, a comment – all of these are forms. This lesson covers input fields, drop-down menus, choosing one or several options, the input checks the browser does by itself and what happens when a form is sent.

Before you start: required prior knowledge – lists and tables, the HTTP protocol.
How to learn: do the tasks at the end on your own in your editor first; open the hint and the solution only after that. Every example has the code and a live result below it; type the code into your own file and change it.

The form element and input fields

<form action="https://httpbin.org/get" method="get">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name" placeholder="e.g. Anna" required>

  <label for="grade">Grade:</label>
  <input type="number" id="grade" name="grade" min="5" max="8" value="7">

  <button type="submit">Send</button>
</form>

Result:

PartMeaning
formthe frame of the form; action – the address the data is sent to, method – GET or POST (see the HTTP protocol)
label for="name"the field's label; clicking the label activates the field with the same id – important for accessibility
namethe name under which the value is sent to the server: ?name=Anna&grade=7 – without a name the field is not sent
placeholdera grey helper text in an empty field (not a replacement for a label)
requiredthe field is required – the browser does not allow sending it empty

In the example the form sends the data to the test service httpbin.org, which returns what it received. Type a name and click "Send" – in the answer you will see "name": "...".

Kinds of fields

<form>
  <p><label>Email: <input type="email" name="email"></label></p>
  <p><label>Password: <input type="password" name="password" minlength="8"></label></p>
  <p><label>Date of birth: <input type="date" name="date"></label></p>
  <p><label>Favourite colour: <input type="color" name="colour" value="#2b7bb9"></label></p>
  <p><label>Rating: <input type="range" name="rating" min="1" max="5"></label></p>
  <p>Language:
    <label><input type="radio" name="language" value="cpp" checked> C++</label>
    <label><input type="radio" name="language" value="py"> Python</label>
  </p>
  <p><label><input type="checkbox" name="news"> I want notifications</label></p>
  <p><label>City:
    <select name="city">
      <option>London</option><option selected>Belgrade</option><option>Paris</option>
    </select></label></p>
  <p><label>Message:<br><textarea name="message" rows="3" cols="30"></textarea></label></p>
  <button>Send</button> <button type="reset">Clear</button>
</form>

Result:

FieldWhen
text, email, password, number, dateentering text, an address, a password (asterisks), a number, a date
radioexactly one option – all the options have the same name
checkboxyes/no or several options at once
select + optiona choice from a drop-down list
textarealonger text over several lines
color, rangechoosing a colour, a slider

On a phone the fields email, number and date open a matching keyboard or calendar – that is why it is important to choose the right type.

Input checks in the browser

AttributeCheck
requiredthe field must not be empty
type="email"must look like an address (has an @)
min, max, stepthe limits of a number
minlength, maxlengththe length of the text
patterna rule written as a regular expression, e.g. pattern="[0-9]{5}" – exactly 5 digits
Important: the check in the browser is only for the user's convenience. Anyone can get around it (F12), so the server must check all the data again – see Web application security.

What happens when a form is sent

  1. The browser checks the fields (required, email…).
  2. It makes name=value pairs from all the fields that have a name.
  3. GET: it adds them to the address (?name=Anna&grade=7) – for searches and filters. POST: it sends them in the body of the request – for logins, registration, messages.
  4. The server processes the data and returns a new page (or JavaScript processes the form itself without sending it – see the DOM and events).

Without a backend a form has nowhere to send the data. That is why on this site's contact page the form opens the email program, while in the Flask and Django lessons the form is received by a Python server.

Practice tasks

Each task has an example, a hint and a solution. The solutions have been checked in the browser.

TASK 01Difficulty: medium

Signing up for the club

Make a form for signing up for the computer club: first and last name (required), email (required), grade (5–8), the language the student wants to learn (radio: C++, Python, Scratch), the days the student can come (checkbox: Tuesday, Thursday) and a note (textarea). The form is sent with the POST method.

Common mistakes and tips
  • A field without a name attribute is not sent to the server.
  • Radio buttons in the same group must have the same name, otherwise several of them can be selected.
  • Never send passwords and personal data with GET – they end up in the address and the browser history.
  • A check in the browser does not replace a check on the server.

Check your knowledge

Open the quiz

Forms

1. What happens to a field that has no name attribute?




2. Which type of field allows choosing exactly one option from a group?




3. Which method is used to send a login form with a password?



4. Is the required check in the browser enough protection?