In ancient Egypt, scribes recorded data on papyri - names, numbers, information about grain deliveries. HTML forms are the modern equivalent of a scribe's papyrus! They are a key tool for collecting information from website users. You can think of them as an interactive interface through which users enter data, which can then be processed, for example, by a server and saved in a database. Forms are essential for many activities, such as logging into a service, registering a new account, or sending messages.
Below we present the basic elements used when creating forms:
<form>: This is the tag that wraps the entire form. All form elements are typically contained within this tag.<input>: This is the most versatile form element. It allows users to enter various types of data - from text, to checking options, to submitting the form. The type of data the user can enter depends on the type attribute of this element.<textarea>: This element allows entering larger amounts of text. It's an ideal place for longer comments or opinions.<select> and <option>: The <select> tag allows creating a dropdown list from which the user can choose one or more options. These options are defined by <option> elements placed inside <select>.<button>: Allows creating buttons with various functions - they can be used to submit a form, reset entered data, or trigger a specific JavaScript function.<label>: This is an element that allows adding a description to other form elements, such as <input>. It's extremely important for the user to understand what data should be entered in a given field.When creating a form, various attributes are often used to identify, label, and fill in default values for individual form fields. Example attributes include
name, id, value, and `Practice this concept in the editor below.
Using the above elements and attributes, we can create forms of varying complexity that will adequately respond to the needs of our users.
A basic form starts with the
<form> tag, which specifies where the data should be sent, using the action and method attributes.1<form action="/submit" method="post">
2 <!-- Form fields -->
3</form>Text input fields are used to collect small amounts of textual information from the user. They are created using the <input> tag.
1<label for="username">Username:</label>
2<input type="text" id="username" name="username">For entering passwords, the password type is used, which causes the entered characters to be hidden.
1<label for="password">Password:</label>
2<input type="password" id="password" name="password">Selection buttons and checkboxes allow users to select options.
1<label for="planet">Choose a planet:</label>
2<select id="planet" name="planet">
3 <option value="mars">Mars</option>
4 <option value="jupiter">Jupiter</option>
5</select>Forms can also handle file uploads, allowing users to send documents, photos, etc.
1<label for="file">Choose a file:</label>
2<input type="file" id="file" name="file">To allow the user to submit the form, you need to add a submit button.
1<input type="submit" value="Submit">Here's an example HTML form that contains various form elements:
1<form>
2 <label for="name">Name:</label>
3 <input type="text" id="name" name="name"
4## Practical Application
5
6Practice this concept in the editor below.
7
8 <label for="email">Email:</label>
9 <input type="email" id="email" name="email"
10## Practical Application
11
12Practice this concept in the editor below.
13
14 <label for="password">Password:</label>
15 <input type="password" id="password" name="password"
16## Practical Application
17
18Practice this concept in the editor below.
19
20 <label for="age">Age:</label>
21 <input type="number" id="age" name="age" min="0" max="120">
22
23 <label for="gender">Gender:</label>
24 <select id="gender" name="gender">
25 <option value="">Choose</option>
26 <option value="female">Female</option>
27 <option value="male">Male</option>
28 <option value="other">Other</option>
29 </select>
30
31 <label for="newsletter">Subscribe to newsletter:</label>
32 <input type="checkbox" id="newsletter" name="newsletter">
33
34 <label for="message">Message:</label>
35 <textarea id="message" name="message"
36## Practical Application
37
38Practice this concept in the editor below.
39
40 <button type="submit">Submit</button>
41 <button type="reset">Clear</button>
42</form>This example form includes various form elements, such as
<input>, <select>, <textarea>, and <button>. The user can enter their name, email address, password, age, gender, decide whether to subscribe to the newsletter, and type a message. After filling out the form, the user can submit it by clicking the "Submit" button or clear the form by clicking the "Clear" button.HTML forms are used everywhere on the internet - from simple contact forms, through registration and login, to advanced payment systems. Mastering form creation is a key skill for every web developer.
In this exercise, you'll learn to create HTML forms, which are a key tool for collecting information from website users. Your task will be to create a web page containing a registration form.
First, create a new HTML file and add the basic HTML document structure.
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <title>Registration Form</title>
7</head>
8<body>
9 <main>
10 <!-- Place your form here -->
11 </main>
12</body>
13</html>Now add a form inside the
<main> element. Start by defining the form and basic text fields.1<form action="/submit" method="post">
2 <label for="name">Name:</label>
3 <input type="text" id="name" name="name"
4## Practical Application
5
6Practice this concept in the editor below.
7
8 <label for="email">Email:</label>
9 <input type="email" id="email" name="email"
10## Practical Application
11
12Practice this concept in the editor below.
13
14 <label for="password">Password:</label>
15 <input type="password" id="password" name="password"
16## Practical Application
17
18Practice this concept in the editor below.
19</form>Expand the form with additional fields, such as number, gender selection, newsletter subscription checkbox, and a textarea for messages.
1<form action="/submit" method="post">
2 <label for="name">Name:</label>
3 <input type="text" id="name" name="name"
4## Practical Application
5
6Practice this concept in the editor below.
7
8 <label for="email">Email:</label>
9 <input type="email" id="email" name="email"
10## Practical Application
11
12Practice this concept in the editor below.
13
14 <label for="password">Password:</label>
15 <input type="password" id="password" name="password"
16## Practical Application
17
18Practice this concept in the editor below.
19
20 <label for="age">Age:</label>
21 <input type="number" id="age" name="age" min="0" max="120">
22
23 <label for="gender">Gender:</label>
24 <select id="gender" name="gender">
25 <option value="">Choose</option>
26 <option value="female">Female</option>
27 <option value="male">Male</option>
28 <option value="other">Other</option>
29 </select>
30
31 <label for="newsletter">Subscribe to newsletter:</label>
32 <input type="checkbox" id="newsletter" name="newsletter">
33
34 <label for="message">Message:</label>
35 <textarea id="message" name="message"
36## Practical Application
37
38Practice this concept in the editor below.
39</form>Add buttons to submit and reset the form.
1<form action="/submit" method="post">
2 <label for="name">Name:</label>
3 <input type="text" id="name" name="name"
4## Practical Application
5
6Practice this concept in the editor below.
7
8 <label for="email">Email:</label>
9 <input type="email" id="email" name="email"
10## Practical Application
11
12Practice this concept in the editor below.
13
14 <label for="password">Password:</label>
15 <input type="password" id="password" name="password"
16## Practical Application
17
18Practice this concept in the editor below.
19
20 <label for="age">Age:</label>
21 <input type="number" id="age" name="age" min="0" max="120">
22
23 <label for="gender">Gender:</label>
24 <select id="gender" name="gender">
25 <option value="">Choose</option>
26 <option value="female">Female</option>
27 <option value="male">Male</option>
28 <option value="other">Other</option>
29 </select>
30
31 <label for="newsletter">Subscribe to newsletter:</label>
32 <input type="checkbox" id="newsletter" name="newsletter">
33
34 <label for="message">Message:</label>
35 <textarea id="message" name="message"
36## Practical Application
37
38Practice this concept in the editor below.
39
40 <button type="submit">Submit</button>
41 <button type="reset">Clear</button>
42</form>After completing all steps, your HTML file should look like this:
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <title>Registration Form</title>
7</head>
8<body>
9 <main>
10 <form action="/submit" method="post">
11 <label for="name">Name:</label>
12 <input type="text" id="name" name="name"
13## Practical Application
14
15Practice this concept in the editor below.
16
17 <label for="email">Email:</label>
18 <input type="email" id="email" name="email"
19## Practical Application
20
21Practice this concept in the editor below.
22
23 <label for="password">Password:</label>
24 <input type="password" id="password" name="password"
25## Practical Application
26
27Practice this concept in the editor below.
28
29 <label for="age">Age:</label>
30 <input type="number" id="age" name="age" min="0" max="120">
31
32 <label for="gender">Gender:</label>
33 <select id="gender" name="gender">
34 <option value="">Choose</option>
35 <option value="female">Female</option>
36 <option value="male">Male</option>
37 <option value="other">Other</option>
38 </select>
39
40 <label for="newsletter">Subscribe to newsletter:</label>
41 <input type="checkbox" id="newsletter" name="newsletter">
42
43 <label for="message">Message:</label>
44 <textarea id="message" name="message"
45## Practical Application
46
47Practice this concept in the editor below.
48
49 <button type="submit">Submit</button>
50 <button type="reset">Clear</button>
51 </form>
52 </main>
53</body>
54</html>Now you have a complete web page with a registration form. You can experiment further by adding more fields, changing attributes, or styling elements with CSS to improve the form's appearance.