HTML Forms

πŸ“¨ Introduction to HTML Forms

HTML forms are used to collect user input on web pages. They are essential for things like login screens, contact forms, surveys, and more. Forms can include text fields, radio buttons, checkboxes, dropdowns, and submit buttons. πŸ“‹

>>β€œForms are the bridge between your users and your application.” πŸŒ‰

🧱 Basic Form Structure

Basic HTML Form

<form action="/submit" method="POST">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name" />

  <br />

  <label for="email">Email:</label>
  <input type="email" id="email" name="email" />

  <br />

  <input type="submit" value="Submit" />
</form>

Note

πŸ’‘ The action attribute defines where the form data should be sent. The method defines how it's sent – usually GET or POST.

🧩 Common Form Elements

  • <input> – Text field, password, checkbox, radio, etc.
  • <textarea> – Multi-line text input
  • <select> – Dropdown list
  • <label> – Describes inputs for accessibility
  • <button> or <input type="submit"> – Submit forms

πŸ“ Input Types

Various Input Types

<input type="text" />        <!-- Single-line input -->
<input type="email" />      <!-- Email validation -->
<input type="password" />   <!-- Hidden input for passwords -->
<input type="radio" />      <!-- Single choice -->
<input type="checkbox" />   <!-- Multiple choice -->
<input type="date" />       <!-- Date picker -->
<input type="submit" />     <!-- Submit button -->

πŸ“¦ Textarea Element

Textarea Example

<label for="message">Your Message:</label>
<textarea id="message" name="message" rows="4" cols="30">
Enter your message here...
</textarea>

⬇️ Select Dropdown

Select Dropdown Example

<label for="fruit">Choose a fruit:</label>
<select id="fruit" name="fruit">
  <option value="apple">Apple</option>
  <option value="banana">Banana</option>
  <option value="cherry">Cherry</option>
</select>

Note

🧠 Always use the <label> element with for to improve accessibility and form usability.

🎯 Form Submission

Forms can be submitted either via GET (appends data to URL) or POST (sends data in request body). Use POST for sensitive information.

MethodWhen to Use
GETSearch queries, filtering, bookmarking
POSTLogin, forms with sensitive data

βœ… Form Best Practices

  • βœ”οΈ Use label for accessibility
  • βœ”οΈ Always validate input on both client and server
  • βœ”οΈ Use semantic type values for better UX
  • βœ”οΈ Add placeholder for hinting input expectations

πŸ”’ Form Validation (Client-side)

Required Field Validation

<input type="email" name="email" required />

Note

⚠️ Client-side validation is helpful but should not replace secure server-side validation.

πŸ“š Additional Resources

>>β€œForms are not just inputsβ€”they’re conversations with your users.” πŸ’¬