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.

Key point:
Forms are how websites talk to users and how users talk back. Without forms, the web would be read-only.

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.

form-basic.html Basic form structure with action and method

Two key attributes on <form>:

  • action — The URL where the form data should be sent. This is often a server endpoint like /api/users or https://example.com/register.
  • method — How to send the data. GET appends data to the URL (good for searches, visible in the address bar). POST sends 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.

labels.html Connecting labels to inputs with id and for

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

input-text-password.html Text and password input types

Email, number, and telephone

input-email-number-tel.html Email, number, and phone inputs with built-in validation

Date, color, and range

input-date-color-range.html Date picker, color picker, and slider

File input

input-file.html File upload 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

checkboxes.html Multiple checkboxes for a single question

Radio buttons — one selection only

radio-buttons.html Radio button group (same name = grouped)
Pro tip:
Use <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/*").
input-attributes.html Using common input attributes

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 — Signup Form
Preview

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.

✏️ Try It Yourself — Survey Form
Preview

Button vs input type submit

You can submit forms with either <button type="submit"> or <input type="submit">. The <button> tag is more flexible because you can put HTML inside it (icons, text styling). The <input type="submit"> is simpler but limited to text.

button-submit.html Different ways to submit forms
Challenge: Ramayai AI Academy Registration Form

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. Use action to set where data goes and method (GET or POST) for how it's sent.
  • Labels are essential for accessibility. Connect them with <label for="id"> matching the input's id.
  • 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, accept all change input behavior.

Test your knowledge

Question 1 of 5
Which attribute connects a <label> to its input field?
Question 2 of 5
What input type hides the text as the user types?
Question 3 of 5
What's the difference between GET and POST form methods?
Question 4 of 5
How do you group radio buttons so only one can be selected at a time?
Question 5 of 5
Which attribute makes a form field mandatory?
0
/5
Great job! You understand form inputs.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

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 →