Bird
Raised Fist0
HTMLmarkup~5 mins

Purpose of forms in HTML

Choose your learning style10 modes available
Introduction

Forms let people send information to a website. They help websites collect data like names, emails, or messages.

When you want users to sign up for a newsletter.
When you need to collect feedback or contact details.
When users must log in with a username and password.
When people fill out surveys or order products online.
When you want to let users search for something on your site.
Syntax
HTML
<form action="URL" method="GET or POST">
  <!-- form fields like input, textarea, select -->
  <button type="submit">Send</button>
</form>

The action attribute tells where to send the form data.

The method attribute decides how data is sent: GET adds data to the URL, POST sends it hidden.

Examples
A simple form to send a username using POST method.
HTML
<form action="/submit" method="POST">
  <input type="text" name="username" placeholder="Your name">
  <button type="submit">Send</button>
</form>
A search form that sends the query in the URL using GET method.
HTML
<form action="/search" method="GET">
  <input type="search" name="query" placeholder="Search here">
  <button type="submit">Search</button>
</form>
Sample Program

This is a simple contact form with fields for name, email, and message. It uses labels for accessibility and placeholders to guide users.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Simple Contact Form</title>
</head>
<body>
  <main>
    <h1>Contact Us</h1>
    <form action="/submit-contact" method="POST" aria-label="Contact form">
      <label for="name">Name:</label><br>
      <input type="text" id="name" name="name" required placeholder="Your full name"><br><br>

      <label for="email">Email:</label><br>
      <input type="email" id="email" name="email" required placeholder="you@example.com"><br><br>

      <label for="message">Message:</label><br>
      <textarea id="message" name="message" rows="4" required placeholder="Write your message here"></textarea><br><br>

      <button type="submit">Send Message</button>
    </form>
  </main>
</body>
</html>
OutputSuccess
Important Notes

Always use <label> tags for better accessibility.

Use required attribute to make sure users fill important fields.

Placeholders give hints but do not replace labels.

Summary

Forms collect information from users to send to websites.

They use <form> with action and method attributes.

Labels and accessibility features help all users interact with forms easily.

Practice

(1/5)
1. What is the main purpose of an HTML <form> element on a webpage?
easy
A. To collect information from users and send it to a server
B. To display images on the webpage
C. To create navigation menus
D. To style the webpage with colors and fonts

Solution

  1. Step 1: Understand the role of the <form> element

    The <form> tag is designed to gather user input like text, selections, or files.
  2. Step 2: Identify what happens with the collected data

    Forms send this data to a server for processing, such as saving or responding.
  3. Final Answer:

    To collect information from users and send it to a server -> Option A
  4. Quick Check:

    Forms collect user data [OK]
Hint: Forms gather and send user info to servers [OK]
Common Mistakes:
  • Thinking forms are for styling or layout
  • Confusing forms with navigation elements
  • Believing forms display images
2. Which of the following is the correct way to start a form that sends data using the POST method to 'submit.php'?
easy
A.
B.
C.
D.

Solution

  1. Step 1: Check the correct attributes for form tag

    The action attribute defines where to send data; method defines how (GET or POST).
  2. Step 2: Verify correct syntax and attribute values

    <form method='post' action='submit.php'> uses method='post' and action='submit.php' correctly and in proper order.
  3. Final Answer:

    <form method='post' action='submit.php'> -> Option A
  4. Quick Check:

    Correct form syntax [OK]
Hint: Use method='post' and action='URL' inside <form> [OK]
Common Mistakes:
  • Using 'get' instead of 'post' when POST is needed
  • Wrong attribute names like 'submit' instead of 'action'
  • Invalid method values like 'send'
3. What will happen when the user clicks the submit button in this form?
<form action='/submit' method='post'>
  <label for='name'>Name:</label>
  <input type='text' id='name' name='username' />
  <button type='submit'>Send</button>
</form>
medium
A. The browser navigates to '/submit' without sending data
B. The form resets all fields to empty
C. The browser sends the entered name to '/submit' using POST method
D. Nothing happens because the button type is incorrect

Solution

  1. Step 1: Understand the form's action and method

    The form sends data to '/submit' using POST when submitted.
  2. Step 2: Check the submit button behavior

    The button with type='submit' triggers sending the form data.
  3. Final Answer:

    The browser sends the entered name to '/submit' using POST method -> Option C
  4. Quick Check:

    Submit button sends data [OK]
Hint: Submit button sends form data to action URL [OK]
Common Mistakes:
  • Thinking submit resets the form
  • Believing data is not sent on submit
  • Confusing button types
4. Identify the error in this form code:
<form action='submit.php' method='post'>
  <input type='text' id='email' name='email'>
  <label for='email'>Email</label>
  <button type='submit'>Submit</button>
</form>
medium
A. The label is not correctly linked to the input
B. The input tag is missing a closing slash
C. The method attribute should be 'get' instead of 'post'
D. The label should come before the input for accessibility

Solution

  1. Step 1: Check label and input order for accessibility

    Labels should appear before inputs or wrap them to help screen readers.
  2. Step 2: Confirm label's for attribute matches input's id

    The label's for='email' matches input's id='email', so linking is correct.
  3. Final Answer:

    The <label> should come before the input for accessibility -> Option D
  4. Quick Check:

    Label before input improves accessibility [OK]
Hint: Place labels before inputs for better accessibility [OK]
Common Mistakes:
  • Ignoring label position and accessibility
  • Thinking input tags always need a closing slash
  • Changing method without reason
5. You want to create a form that collects a user's name and email, ensures both fields are required, and sends data using POST to '/register'. Which of the following is the best complete form code?
hard
A. \n \n \n Submit\n
B. \n Name:\n \n Email:\n \n Register\n
C. \n \n \n Send\n
D. \n Name\n \n Email\n \n Register\n

Solution

  1. Step 1: Check form attributes for sending data

    The form must use action='/register' and method='post' to send data correctly.
  2. Step 2: Verify input fields and required attribute

    Inputs for name and email have matching labels, correct types, and required to ensure user fills them.
  3. Step 3: Confirm button type and labels

    The submit button has type='submit' and labels use for linked to inputs for accessibility.
  4. Final Answer:

    Complete form with action, method, required fields, and labels -> Option B
  5. Quick Check:

    Form with required fields and POST [OK]
Hint: Use required inputs and method='post' with labels for best form [OK]
Common Mistakes:
  • Using GET instead of POST for sensitive data
  • Missing required attribute on inputs
  • Labels not linked to inputs
  • Omitting method attribute
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard