What are HTML forms?
HTML forms let you collect data from users. Every time you fill out a login, sign up for a service, or submit a search query, you're using an HTML form. Forms are the bridge between your web page and your server — they send user input (names, emails, passwords, selections) back to the server for processing.
Forms are made up of several parts: the <form> element (the container), input fields (text, email, password, checkboxes, radio buttons, etc.), labels (accessible descriptions), and a submit button.
The <form> element
The <form> tag wraps all your form inputs. It tells the browser where to send the form data and how to send it.
Two key attributes on <form>:
action— The URL where the form data should be sent. This is often a server endpoint like/api/usersorhttps://example.com/register.method— How to send the data.GETappends data to the URL (good for searches, visible in the address bar).POSTsends data in the request body (good for sensitive data like passwords, hidden from the URL).
Labels and accessibility
The <label> element does two important things: it describes what an input is for, and it improves accessibility. When you click a label, the browser automatically focuses the connected input field.
The best way: use the for attribute on the label to match the input's id. This works for all users, including those using screen readers.
Common input types
The <input> element has many types. Each type creates a different kind of field and lets the browser validate the data automatically.
Text and password inputs
Email, number, and telephone
Date, color, and range
File input
Checkboxes and radio buttons
Checkboxes let users select multiple options. Radio buttons let users select one option from a group. To group radio buttons, give them all the same name attribute.
Checkboxes — multiple selections allowed
Radio buttons — one selection only
<fieldset> and <legend> to group related checkboxes or radio buttons. This improves accessibility for screen readers.
Important input attributes
Input elements have attributes that control their behavior and appearance:
name— The field's identifier when the form is submitted. This is what the server receives.value— The default or submitted value of the input.placeholder— Hint text shown when the field is empty. Disappears when the user types.required— Makes the field mandatory. The form won't submit without it.disabled— Disables the field (user can't interact with it).readonly— User can see the value but can't edit it.autofocus— Focuses this field when the page loads.min/max— For number, date, and range inputs. Sets the allowed range.accept— For file inputs. Specifies allowed file types (e.g.,accept="image/*").
Try it yourself — Signup form
Build a simple signup form with text, email, password, phone, and date fields. All fields have labels and placeholders.
Try it yourself — Survey form
Build a survey form with radio buttons (rating), checkboxes (interests), a color picker, a range slider, and a file upload.
Task: Build a registration form for Ramayai AI Academy with these fields:
- Full Name (text input, required)
- Email (email input, required)
- Phone (tel input)
- Program Selection (radio buttons): Web Development, Data Science, AI & Machine Learning
- Interests (checkboxes): Choose multiple from — Frontend, Backend, Mobile, Data, DevOps
- Start Date (date input, required)
- Subscribe to newsletter (checkbox)
- Submit button labeled "Register Now"
Use the editor above (or modify one of the try-it editors) to build it. Style it nicely with CSS so the form looks professional.
Hint: Start with a <form> wrapper. For each field, use a label + input pair. For program selection (radio buttons), give all radio buttons the same name="program". For interests (checkboxes), give them all the same name="interests". Use <fieldset> and <legend> to group related inputs. Style with CSS to make spacing nice and inputs readable.
Key takeaways
- Forms collect user data with the
<form>element. Useactionto set where data goes andmethod(GET or POST) for how it's sent. - Labels are essential for accessibility. Connect them with
<label for="id">matching the input'sid. - Input types vary: text, email, password, number, tel, date, color, range, file, checkbox, radio. Each type provides built-in validation.
- Group related inputs with
<fieldset>and<legend>for better accessibility and structure. - Radio buttons and checkboxes work differently — radio buttons let you pick one (same name groups them), checkboxes let you pick multiple.
- Attributes matter:
required,placeholder,disabled,readonly,min,max,acceptall change input behavior.
Test your knowledge
You just built an interactive form.
Imagine what you'll build next.
Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses →