In this section, we will learn how to create a basic HTML form. Forms are essential for collecting user input and are widely used in web applications. We will cover the following topics:
- Form Structure
- Form Elements
- Form Attributes
- Practical Example
- Exercises
- Form Structure
A form in HTML is defined using the <form>
tag. The basic structure of a form looks like this:
action
: Specifies the URL where the form data should be sent when the form is submitted.method
: Specifies the HTTP method to use when sending form data. Common methods areGET
andPOST
.
- Form Elements
Forms can contain various types of input elements, such as text fields, checkboxes, radio buttons, and submit buttons. Here are some common form elements:
- Text Input:
<input type="text" name="username">
- Password Input:
<input type="password" name="password">
- Submit Button:
<input type="submit" value="Submit">
- Form Attributes
Forms and form elements can have various attributes to control their behavior and appearance:
name
: Identifies the form element when data is submitted.value
: Specifies the initial value of the form element.placeholder
: Provides a hint to the user about what to enter in the input field.required
: Specifies that the input field must be filled out before submitting the form.
- Practical Example
Let's create a simple login form with username and password fields:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Basic Form Example</title> </head> <body> <h1>Login Form</h1> <form action="submit_form.php" method="post"> <label for="username">Username:</label> <input type="text" id="username" name="username" placeholder="Enter your username" required> <br><br> <label for="password">Password:</label> <input type="password" id="password" name="password" placeholder="Enter your password" required> <br><br> <input type="submit" value="Login"> </form> </body> </html>
Explanation:
<label>
: Defines a label for an input element. Thefor
attribute should match theid
of the input element.<input type="text">
: Creates a text input field.<input type="password">
: Creates a password input field.<input type="submit">
: Creates a submit button.
- Exercises
Exercise 1: Create a Registration Form
Create a registration form with the following fields:
- First Name (text)
- Last Name (text)
- Email (email)
- Password (password)
- Confirm Password (password)
- Submit Button
Solution:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Registration Form</title> </head> <body> <h1>Registration Form</h1> <form action="register.php" method="post"> <label for="first_name">First Name:</label> <input type="text" id="first_name" name="first_name" placeholder="Enter your first name" required> <br><br> <label for="last_name">Last Name:</label> <input type="text" id="last_name" name="last_name" placeholder="Enter your last name" required> <br><br> <label for="email">Email:</label> <input type="email" id="email" name="email" placeholder="Enter your email" required> <br><br> <label for="password">Password:</label> <input type="password" id="password" name="password" placeholder="Enter your password" required> <br><br> <label for="confirm_password">Confirm Password:</label> <input type="password" id="confirm_password" name="confirm_password" placeholder="Confirm your password" required> <br><br> <input type="submit" value="Register"> </form> </body> </html>
Exercise 2: Create a Feedback Form
Create a feedback form with the following fields:
- Name (text)
- Email (email)
- Feedback (textarea)
- Submit Button
Solution:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Feedback Form</title> </head> <body> <h1>Feedback Form</h1> <form action="feedback.php" method="post"> <label for="name">Name:</label> <input type="text" id="name" name="name" placeholder="Enter your name" required> <br><br> <label for="email">Email:</label> <input type="email" id="email" name="email" placeholder="Enter your email" required> <br><br> <label for="feedback">Feedback:</label> <textarea id="feedback" name="feedback" placeholder="Enter your feedback" required></textarea> <br><br> <input type="submit" value="Submit"> </form> </body> </html>
Conclusion
In this section, we have learned how to create a basic HTML form, including the structure, common form elements, and attributes. We also provided practical examples and exercises to reinforce the concepts. In the next section, we will dive deeper into form elements such as input, textarea, and select.
HTML Course
Module 1: Introduction to HTML
- What is HTML?
- Setting Up Your Environment
- Basic HTML Structure
- HTML Tags and Elements
- Creating Your First HTML Page
Module 2: HTML Text Formatting
- Headings and Paragraphs
- Text Formatting Tags
- Lists: Ordered and Unordered
- Blockquotes and Preformatted Text
Module 3: HTML Links and Media
Module 4: HTML Tables
Module 5: HTML Forms
- Creating a Basic Form
- Form Elements: Input, Textarea, and Select
- Form Attributes and Validation
- Submitting Forms