The Wayback Machine - https://web.archive.org/web/20240125233121/https://www.geeksforgeeks.org/html-forms/
Open In App
Related Articles

HTML Forms

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

HTML stands for Hyper Text Markup Language. It is used to design web pages using a markup language. It is a combination of Hypertext and Markup language. HTML uses predefined tags and elements that tell the browser how to properly display the content on the screen, and form is one of them. So, in this article, we will learn what is exactly HTML form, what are the elements of forms and how can we use HTML form in our webpage. 

What is HTML <form>?

<form> is an HTML element to collect input data containing interactive controls. It provides facilities to input text, numbers, values, email, password, and control fields such as checkboxes, radio buttons, submit buttons, etc., or in other words, form is a container that contains input elements like text, email, number, radio buttons, checkboxes, submit buttons, etc. It also :

  • Facilitates user input collection through various elements.
  • Utilizes <form> tags to structure input elements.
  • Defines actions for data submission upon form completion.
  • Supports client-side validation for enhanced user experience.

Syntax:

<form>
<!--form elements-->
</form>

Form elements

These are the following HTML <form> elements:

  • <label>: It defines label for <form> elements.
  • <input>: It is used to get input data from the form in various types such as text, password, email, etc by changing its type.
  • <button>: It defines a clickable button to control other elements or execute a functionality.
  • <select>: It is used to create a drop-down list.
  • <textarea>: It is used to get input long text content.
  • <fieldset>: It is used to draw a box around other form elements and group the related data.
  • <legend>: It defines a caption for fieldset elements.
  • <datalist>: It is used to specify pre-defined list options for input controls.
  • <output>: It displays the output of performed calculations.
  • <option>: It is used to define options in a drop-down list.
  • <optgroup>: It is used to define group-related options in a drop-down list.

Textbox in HTML Form

In an HTML form, we use the <input> tag by assigning type attribute value to text to input single line input. To define type attribute see the below syntax. 

Tip: The default value of the type attribute is “text“.

Syntax:

<input type="text" />

// Or shorthand for "text" type:

<input />

Password in an HTML Form

We can change the type value text to password to get the input password 

Example: This example shows the type password in html form.We can see the difference between type text and type password. The username will be visible but the password will not be visible. 

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>Page Title</title>
    <style>
        body {
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: Arial, sans-serif;
            line-height: 1.8;
            min-height: 100vh;
            background: #ffffff;
            flex-direction: column;
        }
 
        /* Main container styling */
        .main {
            background-color: #fff;
            border-radius: 15px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
            padding: 20px;
            transition: transform 0.2s;
            width: 600px;
        }
 
        form {
            display: flex;
            align-items: center;
            justify-content: center;
            flex-direction: column;
        }
 
        h1 {
            text-align: center;
            color: green;
        }
 
        input {
            padding: 10px;
        }
    </style>
</head>
 
<body>
    <div class="main">
        <h1>Welcome To GFG</h1>
        <form>
 
            <p>
                <label>Username :
                    <input type="text" /></label>
            </p>
 
            <p>
                <label>Password :
                    <input type="password" /></label>
            </p>
 
            <p>
                <button type="submit">Submit</button>
            </p>
 
        </form>
    </div>
</body>
 
</html>


Output:

ac

Output

Radio Button in an HTML Form

To create a radio button, we use the <input> tag following by radio type to provide users to choose a limited number of choices. The value attribute defines the unique value associated with each radio button. The value is not shown to the user, but is the value that is sent to the server on “submit” to identify which radio button that was selected.

 Syntax:

<input type="radio" name="radio_button_name" value="radio_button_value" />

Note: The radio button must have shared the same name to be treated as a group. 

Example: In this example, we will create a radio button to choose your gender. 

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>Page Title</title>
    <style>
        body {
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: Arial, sans-serif;
            line-height: 1.8;
            min-height: 100vh;
            background: #ffffff;
            flex-direction: column;
        }
 
        /* Main container styling */
        .main {
            background-color: #fff;
            border-radius: 15px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
            padding: 20px;
            transition: transform 0.2s;
            width: 600px;
        }
 
        form {
            display: flex;
            align-items: center;
            justify-content: center;
            flex-direction: column;
        }
 
        h1,
        h3 {
            text-align: center;
            color: green;
        }
 
        input {
            padding: 10px;
        }
    </style>
</head>
 
<body>
    <div class="main">
        <h1>Welcome To GFG</h1>
        <h3>Choose Your Gender</h3>
        <form>
            <label>
                Male
                <input type="radio" '
                name="gender" value="male" />
            </label>
            <label>
                Female
                <input type="radio"
                name="gender" value="female" />
            </label>
        </form>
    </div>
</body>
 
</html>


Output:

Screenshot-2023-11-16-163104

Output

Checkbox in an HTML Form

To create a checkbox in an HTML form, we use the <input> tag followed by the input type checkbox. It is a square box to tick to activate this. It used to choose more options at a time. 

Syntax:

<input type="checkbox" name="select_box_name" value="select_box_value" />

Note: the “name” and “value” attributes are used to send the checkbox data to the server.

Example: In this example, we use checkboxes to select language. 

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>Page Title</title>
    <style>
        body {
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: Arial, sans-serif;
            line-height: 1.8;
            min-height: 100vh;
            background: #ffffff;
            flex-direction: column;
        }
 
        /* Main container styling */
        .main {
            background-color: #fff;
            border-radius: 15px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
            padding: 20px;
            transition: transform 0.2s;
            width: 600px;
        }
 
        form {
            display: flex;
            align-items: center;
            justify-content: center;
            flex-direction: column;
        }
 
        h1,
        h3 {
            text-align: center;
            color: green;
        }
 
        input {
            padding: 10px;
        }
    </style>
</head>
 
<body>
    <div class="main">
        <h1>Welcome To GFG</h1>
        <h3>Choose Language</h3>
        <form>
            <ul style="list-style-type:none;">
                <li><input type="checkbox"
                    name="language" value="hindi" />
                    Hindi
                </li>
                <li><input type="checkbox"
                    name="language" value="english" />
                    English
                </li>
                <li><input type="checkbox"
                    name="language" value="sanskrite" />
                    Sanskrit
                </li>
            </ul>
        </form>
    </div>
</body>
 
</html>


Output:

Screenshot-2023-11-16-163345

Output

Combobox in an HTML Form

Combobox is used to create a drop-down menu in your form which contains multiple options. So, to create a Combobox in an HTML form, we use the <select> tag with <option> tag. It is also known as a drop-down menu. 

Syntax:

<select name="select_box_name">
<option value="value1">option1</option>
<option value="value2">option2</option>
<option value="value3">option3</option>
</select>

Note: the “name” and “value” attributes are used to send the Combobox data to the server.

Example: In this example, we will create a dropdown menu to select Nationality. 

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>Page Title</title>
    <style>
        body {
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: Arial, sans-serif;
            line-height: 1.8;
            min-height: 100vh;
            background: #ffffff;
            flex-direction: column;
        }
 
        /* Main container styling */
        .main {
            background-color: #fff;
            border-radius: 15px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
            padding: 20px;
            transition: transform 0.2s;
            width: 600px;
        }
 
        form {
            display: flex;
            align-items: center;
            justify-content: center;
            flex-direction: column;
        }
 
        h1,
        h3 {
            text-align: center;
            color: green;
        }
 
        input {
            padding: 10px;
        }
 
        /* for giving padding to options */
        select {
            padding: 10px;
        }
    </style>
</head>
 
<body>
    <div class="main">
        <h1>Welcome To GFG</h1>
        <h3>Choose Your Nationality</h3>
        <form>
            <select name="language">
                <option value="indian">
                  Indian
                  </option>
                <option value="nepali">
                  Nepali
                  </option>
                <option value="others">
                  Others
                  </option>
            </select>
        </form>
    </div>
</body>
 
</html>


Output:

af

Output

Submit button in an HTML Form

In the HTML form, submit button is used to submit the details of the form to the form handler. A form handler is a file on the server with a script that is used to process input data.  

Syntax:

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

Example: In this example, we will create a submit button. 

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>Page Title</title>
    <style>
        body {
            margin-left: 50px;
            font-family: Arial, sans-serif;
            line-height: 1.8;
            min-height: 100vh;
            background: #ffffff;
            flex-direction: column;
        }
 
        /* Main container styling */
        .main {
            background-color: #fff;
            border-radius: 15px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
            padding: 20px;
            transition: transform 0.2s;
            width: 600px;
        }
 
        form {
            display: flex;
            align-items: center;
            justify-content: center;
            flex-direction: column;
        }
 
        h1,
        h3 {
            text-align: center;
            color: green;
        }
 
        input,
        button {
            padding: 10px;
        }
    </style>
</head>
 
<body>
    <div class="main">
        <h1>Welcome To GFG</h1>
        <form onsubmit="showAlert(event)">
            <p>
                <label>
                    Username:
                    <input type="text" />
                </label>
            </p>
            <p>
                <label>
                    Password:
                    <input type="password" />
                </label>
            </p>
            <p>
                <button type="submit">Submit</button>
            </p>
        </form>
    </div>
 
    <script>
        function showAlert(event) {
            // Prevents the default form submission
            event.preventDefault();
            alert('Form submitted!');
        }
    </script>
</body>
 
</html>


Output:

ae

Output

TextArea in an HTML Form

In the HTML form, a text area is used to add comments or reviews, or addresses to the form, in other words, the text area is a multi-line text input control. It contains an unlimited number of characters, the text renders in a fixed-width font, and the size of the text area is given by the <rows> and <cols> attributes. To create a text area in the form use the <textarea> tag.

Syntax:

<textarea name="textarea_name">content</textarea>

Note: the name attribute is used to reference the textarea data after it is sent to a server.

Example: In this example, we will create a textarea. 

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>Page Title</title>
    <style>
        body {
            display: flex;
            align-items: center;
            justify-content: center;
            margin-left: 50px;
            font-family: Arial, sans-serif;
            line-height: 1.8;
            min-height: 100vh;
            background: #ffffff;
            flex-direction: column;
        }
 
        /* Main container styling */
        .main {
            background-color: #fff;
            border-radius: 15px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
            padding: 20px;
            transition: transform 0.2s;
            width: 600px;
        }
 
        form {
            display: flex;
            align-items: center;
            justify-content: center;
            flex-direction: column;
        }
 
        h1,
        h3 {
            text-align: center;
            color: green;
        }
 
        input,
        button {
            padding: 10px;
        }
    </style>
</head>
 
<body>
    <div class="main">
        <h1>Welcome To GeeksforGeeks</h1>
        <form>
            <textarea name="welcomeMessage"
                      rows="23" cols="40">
              GeeksforGeeks is a online portal
          </textarea>
        </form>
    </div>
</body>
 
</html>


Output:

Screenshot-2023-11-16-164940

Output

Create an HTML form to input the basic details of a student

In this example, we will take input such as Salutation, First Name, Last Name, Email, Phone, Gender, Date of Birth, and Address. 

To create this form, we need to use the <legend> tag to define the caption, <select> tag for Salutation, <option> tag to define elements of Salutation, <input> tag for First Name, Last Name, Email, Phone, Date of Birth by changing <input> tag type attribute, <textarea> to input address, the radio button for gender. After defining all these stuff, we will use a <button> to submit this form data. 

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>Page Title</title>
    <style>
        body {
            display: flex;
            align-items: center;
            justify-content: center;
            margin-left: 50px;
            font-family: Arial, sans-serif;
            line-height: 1.8;
            min-height: 100vh;
            background: #ffffff;
            flex-direction: column;
        }
 
        /* Main container styling */
        .main {
            background-color: #fff;
            border-radius: 15px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
            padding: 20px;
            transition: transform 0.2s;
            width: 600px;
        }
 
        form {
            display: flex;
            align-items: center;
            justify-content: center;
            flex-direction: column;
        }
 
        h1,
        h3 {
            text-align: center;
            color: green;
        }
 
        input,
        button {
            padding: 10px;
        }
    </style>
</head>
 
<body>
    <div class="main">
        <h1>Welcome To GeeksforGeeks</h1>
        <form>
            <fieldset>
                <legend>Personal Details</legend>
 
                <p>
                    <label>
                        Salutation
                        <br />
                        <select name="salutation">
                            <option>--None--</option>
                            <option>Mr.</option>
                            <option>Ms.</option>
                            <option>Mrs.</option>
                            <option>Dr.</option>
                            <option>Prof.</option>
                        </select>
                    </label>
                </p>
 
                <p>
                    <label>First name:
                      <input name="firstName" />
                      </label>
                </p>
 
                <p>
                    <label>Last name:
                      <input name="lastName" />
                      </label>
                </p>
 
                <p>
                    Gender :
                    <label><input type="radio"
                                  name="gender" value="male" />
                        Male
                    </label>
                    <label><input type="radio"
                                  name="gender" value="female" />
                        Female
                    </label>
                </p>
 
                <p>
                    <label>Email:
                        <input type="email" name="email" />
                    </label>
                </p>
 
                <p>
                    <label>Date of Birth:
                      <input type="date" name="birthDate">
                    </label>
                </p>
 
                <p>
                    <label>
                        Address :
                        <br />
                        <textarea name="address"
                                  cols="30" rows="3">
                          </textarea>
                    </label>
                </p>
 
                <p>
                    <button type="submit">Submit</button>
                </p>
 
            </fieldset>
    </div>
</body>
 
</html>


Output:

Screenshot-2023-11-16-165250

Output


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 19 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials