Forms let people send information to a website. They help websites collect data like names, emails, or messages.
Form structure in HTML
Introduction
Syntax
HTML
<form action="URL" method="GET|POST"> <!-- form controls like inputs, buttons go here --> </form>
action tells where the form data goes when submitted.
method decides how data is sent: GET adds data to the URL, POST sends it hidden.
Examples
HTML
<form action="/submit" method="POST"> <input type="text" name="username"> <button type="submit">Send</button> </form>
HTML
<form action="/search" method="GET"> <input type="search" name="query"> <button type="submit">Search</button> </form>
Sample Program
This is a complete contact form with name, email, and message fields. It uses labels for accessibility and requires all fields before sending.
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>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><br><br> <label for="email">Email:</label><br> <input type="email" id="email" name="email" required><br><br> <label for="message">Message:</label><br> <textarea id="message" name="message" rows="4" required></textarea><br><br> <button type="submit">Send Message</button> </form> </main> </body> </html>
Important Notes
Always use <label> with inputs for better accessibility.
Use required attribute to make sure users fill important fields.
Use semantic elements like <main> and proper heading levels for structure.
Summary
Forms collect user information and send it to a server.
The <form> tag wraps all inputs and buttons.
Use action and method to control where and how data is sent.
Practice
1. What is the main purpose of the
<form> element in HTML?easy
Solution
Step 1: Understand the role of the <form> tag
The <form> element groups input fields and buttons to collect user data.Step 2: Identify the purpose of forms
Forms send collected data to a server for processing, like login or signup.Final Answer:
To collect user input and send it to a server -> Option DQuick Check:
Form = collect and send data [OK]
Hint: Forms wrap inputs to send data to servers [OK]
Common Mistakes:
- Confusing forms with layout elements
- Thinking forms style content
- Assuming forms display images
2. Which attribute in the
<form> tag specifies where the form data is sent?easy
Solution
Step 1: Identify the purpose of the 'action' attribute
The 'action' attribute defines the URL where form data is sent after submission.Step 2: Differentiate from other attributes
'method' defines how data is sent (GET or POST), 'type' and 'name' are not for destination.Final Answer:
action -> Option CQuick Check:
action = destination URL [OK]
Hint: Action sets form data destination URL [OK]
Common Mistakes:
- Confusing 'method' with 'action'
- Using 'type' instead of 'action'
- Omitting the 'action' attribute
3. What will happen when this form is submitted?
<form action='/submit' method='post'> <input type='text' name='username' /> <button type='submit'>Send</button> </form>
medium
Solution
Step 1: Analyze the form attributes
The form uses 'action="/submit"' and 'method="post"', so data goes to '/submit' via POST.Step 2: Understand form submission behavior
On clicking the submit button, the browser sends the input data as POST to the specified URL.Final Answer:
The browser sends the username data to '/submit' using POST method -> Option AQuick Check:
action + method = POST to /submit [OK]
Hint: Method POST sends data to action URL on submit [OK]
Common Mistakes:
- Assuming GET method when POST is set
- Thinking JavaScript handles submission automatically
- Believing page reloads without sending data
4. Identify the error in this form code:
<form action='/send' method='post'> <input type='text' name='email'> <button type='submit'>Submit</button> </form>
medium
Solution
Step 1: Check input tag syntax
The input tag is <input type='text' name='email'> without self-closing slash.Step 2: Understand HTML5 input tag rules
In HTML5, void elements like input do not require a closing tag or slash; both <input ...> and <input ... /> are valid.Step 3: Verify other elements
Form has action and method; button is type='submit'. No errors present.Final Answer:
There is no error in the code -> Option BQuick Check:
Void tags like input don't need closing slash in HTML5 [OK]
Hint: Void tags like input don't need closing slash in HTML5 [OK]
Common Mistakes:
- Thinking input must always have a closing slash
- Confusing button types
- Missing method attribute (actually present)
5. You want a form that sends data to '/register' using GET method and includes a text input for 'name' and a submit button. Which code is correct?
hard
Solution
Step 1: Check the form's action attribute
The form must send data to '/register', so action='/register' is required.Step 2: Verify the method attribute
The method should be 'get' to send data via URL parameters.Step 3: Confirm input and button correctness
The input has name='name' and the button is type='submit', which is correct.Final Answer:
Form with action='/register' and method='get' -> Option AQuick Check:
action='/register' + method='get' = <form action='/register' method='get'> <input type='text' name='name' /> <button type='submit'>Register</button> </form> [OK]
Hint: Use action for URL and method='get' for URL parameters [OK]
Common Mistakes:
- Omitting action attribute
- Using wrong method (post instead of get)
- Missing submit button type
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
