Creating and decorating forms on web pages
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.
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.
Teachers who write surveys, sociologists and web developers decide the type of answer in advance.
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.
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>
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>
“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.