AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Contents · Computing

Forms on web pages

Lessons 21 · 1 lessons · N. I. Taylaqov (general editor), A. B. Akhmedov, M. D. Pardayeva, A. A. Abdug‘aniyev, U. M. Mirsanov. Informatics and Information Technologies, Grade 11, 1st edition. “Extremum-press”, Tashkent, 2018
21

Creating and decorating forms on web pages

Textbook: pp. 77–80
GoalCreates an HTML form (form, input, label, button), uses attributes that check the entered data and builds a result from the entered texts with JavaScript; knows the rules for collecting personal data.
New words
<form>: the container of a form; its action says where the data goes and method how · <form><input>: an input field; its type may be text, number, email, password, checkbox, radio and more · <input>validation: checking that entered data is complete and correct · validatsiya<label>: the caption of a field, linked to it with for and id; it helps screen readers and clicking · <label>
Explanation

A form is used to get data from the user: registration, a questionnaire, search, a calculator. In HTML a form is wrapped in the <form> tag; inside there are <input> fields, <textarea> (multi-line text), <select> (a list) and <button>. The type of an input is given by the type attribute: text, number, email, password (characters are hidden), checkbox and radio (choices), date. The name attribute of each field is the key under which the data is sent to the server; <label for="ism"> ties a caption to the field with id="ism". The form is sent to the action address with method="post" or "get"; GET shows the data in the URL, so POST and HTTPS are used for passwords or personal data. Before sending, the browser checks data with attributes such as required, min/max, maxlength and type="email"; this validation is for convenience, but a user can get around it, so the server must also check the data again. JavaScript can also work with the typed text: the value property gives the field's value and textContent shows the result. When creating a form, ask only for the data that is needed; work with personal data is regulated by the Law on Personal Data (2019), and in learning exercises enter invented data rather than real people's data.

💡 Where will I use this?
In real life

In a school survey the answers for class are given as a list (Year 5, Year 6 and so on) and the pupil picks rather than writes. Then everyone answers in the same way and counting the answers is easy.

Who uses it

Teachers who write surveys, sociologists and web developers decide the type of answer in advance.

Try it today

Make a mini-survey on paper: one short written line, one choose-from-a-list question and one yes/no question. Ask a classmate to fill it in.

Worked examples
Html
1<!DOCTYPE html>
2<html lang="uz">
3<head>
4  <meta charset="utf-8">
5  <title>Klubga yozilish</title>
6</head>
7<body>
8  <form action="/join" method="post">
9    <p><label for="ism">Ism:</label>
10       <input type="text" id="ism" name="ism" required maxlength="30"></p>
11    <p><label for="yil">Tug‘ilgan yil:</label>
12       <input type="number" id="yil" name="yil" min="2005" max="2012" required></p>
13    <p><label for="pochta">E-pochta (ixtiyoriy):</label>
14       <input type="email" id="pochta" name="pochta"></p>
15    <button type="submit">Yuborish</button>
16  </form>
17</body>
18</html>
A club sign-up form with checks – code above. If the name is empty or the year is outside 2005–2012, the browser does not let it be sent; the number of valid values for the year is 2012 – 2005 + 1 = 8.
Html
1<!DOCTYPE html>
2<html lang="uz">
3<head>
4  <meta charset="utf-8">
5  <title>Qisqa yozuv</title>
6</head>
7<body>
8  <p>Familiya: <input type="text" id="fam"></p>
9  <p>Ism: <input type="text" id="ism"></p>
10  <button onclick="qisqa()">Hosil qil</button>
11  <p id="chiqish"></p>
12  <script>
13    function qisqa() {
14      var f = document.getElementById("fam").value.trim();
15      var i = document.getElementById("ism").value.trim();
16      var out = document.getElementById("chiqish");
17      if (f === "" || i === "") {
18        out.textContent = "Ikkala maydonni to‘ldiring";
19      } else {
20        out.textContent = f + " " + i.charAt(0).toUpperCase() + ".";
21      }
22    }
23  </script>
24</body>
25</html>
A JavaScript form that builds a short entry from a surname and first name – code above. If “Karimov” and “aziza” are entered, the result is “Karimov A.”; if the first name is empty, the message “Ikkala maydonni to‘ldiring” (“Fill in both fields”) appears.
Class activity

“Testing the form”: a pair opens the form, deliberately enters wrong values (an empty name, the year 1999, the e-mail “abc”) and writes down which message the browser gives; then they compare the messages with classmates.

Practice
1
Name five types of input.
2
Values from 2005 to 2012 (both included) may be entered in the year field. How many values are valid?
3
In the short-entry form of the example, if the surname is “Yusupov” and the first name is “bobur”, what is the result?
4
Why must the server check the data again even though the browser has checked it?
Story of the Week · The Khwarizmi WayThe open-day planClose the week: all of this week’s subjects in one storyRead →