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.
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.
Basic knowledge of HTML is required for this tutorial.
<form>
ElementThe <form>
element acts as a container for all the different input elements, like text fields, checkboxes, radio-buttons, submit buttons, etc.
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.
The submit button is used to send the form data to a server. This is created using the <input>
element with type="submit"
.
<!-- 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 -->
<label>
tag defines a label for the input element. The for
attribute associates the label with a specific input.<input type="text">
defines a one-line input field that a user can enter text into.<input type="submit">
defines a button for submitting the form data to the server.<!-- 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 -->
<input type="radio">
is used for radio buttons, which allow the user to select one option from a set.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.
Create an HTML form that includes the following fields:
- First Name (text)
- Last Name (text)
- Email (email)
- Password (password)
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.
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!