Welcome back! Youβve learned how to build structured pages, add images, create links and buttons, and organize data with tables. Now, letβs learn how to make your webpage interactive by collecting information from visitors using HTML forms.
Forms are essential for sign-ups, surveys, contact pages, and much more.
π What is an HTML Form?
A form is a section of your webpage that collects user input. Itβs created using the <form>
tag and includes various types of input fields.
π§© Basic Form Structure
<form action="submit.php" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<button type="submit">Submit</button>
</form>
π Important Attributes
Attribute | Meaning |
---|---|
action | Where to send the form data when submitted. |
method | How to send data: get (URL) or post (hidden in HTTP body). |
π‘ Common Input Types
Text Field
<input type="text" name="username">
Email Field
<input type="email" name="user_email">
Password Field
<input type="password" name="user_password">
Radio Buttons
<input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female
Checkboxes
<input type="checkbox" name="subscribe" value="yes"> Subscribe to newsletter
Dropdown (Select)
<select name="country">
<option value="india">India</option>
<option value="usa">USA</option>
<option value="uk">UK</option>
</select>
Textarea (Multi-line)
<textarea name="message" rows="4" cols="50"></textarea>
π Buttons
<button type="submit">Submit</button>
<button type="reset">Reset</button>
β¨ Example: Complete Contact Form
<form action="submit-form.php" method="post">
<h2>Contact Us</h2>
<label for="fullname">Full Name:</label><br>
<input type="text" id="fullname" name="fullname" required><br><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email" required><br><br>
<label for="message">Your Message:</label><br>
<textarea id="message" name="message" rows="5" cols="40"></textarea><br><br>
<label>
<input type="checkbox" name="subscribe" value="yes"> Subscribe to our newsletter
</label><br><br>
<button type="submit">Send Message</button>
</form>
π‘ Tips for Forms
β
Always label your input fields clearly for better accessibility.
β
Use required
to ensure important fields must be filled before submission.
β
Use correct input types (email
, password
, etc.) to improve user experience and validation.
β
Use method="post"
when submitting sensitive data.
β Summary
- Forms help you collect data from users.
- Use various input types like text, email, radio buttons, checkboxes, and dropdowns.
- Always provide clear labels and use attributes like
required
for validation. - Style your forms with CSS to make them more appealing.
π Whatβs Next?
In the next blog, weβll learn how to embed videos and other media in your web pages to make them more dynamic and engaging. π₯β¨
Stay tuned β and keep building awesome web pages! π