Creating a Basic HTML Form

Tutorial 1 of 5

1. Introduction

1.1 Tutorial's Goal

In this tutorial, we aim to guide you through creating a basic HTML form. HTML forms are essential for collecting user input on a webpage.

1.2 Learning Outcomes

By the end of this tutorial, you will be able to:
- Understand the <form> element and its importance.
- Create different types of input fields.
- Use the <textarea>, <button>, and <select> elements.

1.3 Prerequisites

Basic knowledge of HTML is required for this tutorial.

2. Step-by-Step Guide

2.1 The <form> Element

The <form> element acts as a container for all the different input elements, like text fields, checkboxes, radio-buttons, submit buttons, etc.

2.2 Form Inputs

There are different types of input fields that you can include in your form. These are created using the <input> element. The type attribute of the <input> element determines what kind of input field to display.

2.3 Submit Button

The submit button is used to send the form data to a server. This is created using the <input> element with type="submit".

3. Code Examples

3.1 Simple Form with Text Input

<!-- Start of Form -->
<form>
    <!-- Label for the Text Input -->
    <label for="fname">First name:</label><br>
    <!-- Text Input Field -->
    <input type="text" id="fname" name="fname"><br>
    <!-- Submit Button -->
    <input type="submit" value="Submit">
</form>
<!-- End of Form -->
  • The <label> tag defines a label for the input element. The for attribute associates the label with a specific input.
  • The <input type="text"> defines a one-line input field that a user can enter text into.
  • The <input type="submit"> defines a button for submitting the form data to the server.

3.2 Form with Radio Buttons

<!-- Start of Form -->
<form>
    <!-- Label and Radio Button -->
    <input type="radio" id="male" name="gender" value="male">
    <label for="male">Male</label><br>
    <input type="radio" id="female" name="gender" value="female">
    <label for="female">Female</label><br>
    <!-- Submit Button -->
    <input type="submit" value="Submit">
</form>
<!-- End of Form -->
  • The <input type="radio"> is used for radio buttons, which allow the user to select one option from a set.

4. Summary

In this tutorial, we learned how to create a basic HTML form using the <form>, <input>, and <label> elements. We also learned about different types of input fields such as text fields and radio buttons.

5. Practice Exercises

5.1 Exercise 1

Create an HTML form that includes the following fields:
- First Name (text)
- Last Name (text)
- Email (email)
- Password (password)

5.2 Exercise 2

Create an HTML form with the following fields:
- Gender (radio buttons with options "Male", "Female", and "Other")
- Favorite Programming Language (dropdown with options "JavaScript", "Python", "C++", "Java")

Remember to include a submit button in each form.

6. Solutions and Tips

Solution to exercise 1:

<form>
    <label for="fname">First Name:</label><br>
    <input type="text" id="fname" name="fname"><br>
    <label for="lname">Last Name:</label><br>
    <input type="text" id="lname" name="lname"><br>
    <label for="email">Email:</label><br>
    <input type="email" id="email" name="email"><br>
    <label for="pwd">Password:</label><br>
    <input type="password" id="pwd" name="pwd"><br>
    <input type="submit" value="Submit">
</form>

Solution to exercise 2:

<form>
    <input type="radio" id="male" name="gender" value="male">
    <label for="male">Male</label><br>
    <input type="radio" id="female" name="gender" value="female">
    <label for="female">Female</label><br>
    <input type="radio" id="other" name="gender" value="other">
    <label for="other">Other</label><br>
    <label for="lang">Favorite Programming Language:</label><br>
    <select id="lang" name="lang">
        <option value="js">JavaScript</option>
        <option value="python">Python</option>
        <option value="cpp">C++</option>
        <option value="java">Java</option>
    </select>
    <input type="submit" value="Submit">
</form>

Keep practicing and experimenting with different types of input fields. Happy coding!