HTML Tutorial menu

HTML Forms

Forms collect information from visitors, such as a name, a phone number or a message.

The form Element

All form fields go inside a <form> element. The action attribute says where the data is sent, and method says how (get or post).

Example

html
<form action="/register" method="post">
  ...form fields...
</form>

A form needs a server program (in PHP, Node.js, Python and so on) to actually receive and save the data. HTML only builds the form itself.

Text Fields and Labels

<input type="text"> makes a one-line text box. <label> gives it a visible name. Connect them by giving the input an id and the label a matching for.

Example

html
<!DOCTYPE html>
<html>
<body>

<form>
  <label for="fname">First name:</label><br>
  <input type="text" id="fname" name="fname" placeholder="e.g. Ayesha"><br><br>
  <label for="city">City:</label><br>
  <input type="text" id="city" name="city" value="Karachi">
</form>

</body>
</html>
Try it Yourself »
AttributePurpose
nameThe name sent to the server with the value. Without it, the field is not sent
idConnects the field to its label
valueA starting value
placeholderA grey hint shown while the field is empty

Clicking a connected label puts the cursor in its field, and screen readers read the label aloud.

Radio Buttons and Checkboxes

Radio buttons let the visitor choose one option. All radios in a group share the same name. Checkboxes let them choose zero or more.

Example

html
<!DOCTYPE html>
<html>
<body>

<form>
  <p>Preferred batch:</p>
  <input type="radio" id="morning" name="batch" value="morning">
  <label for="morning">Morning</label><br>
  <input type="radio" id="evening" name="batch" value="evening" checked>
  <label for="evening">Evening</label>

  <p>Courses you are interested in:</p>
  <input type="checkbox" id="html" name="course" value="html">
  <label for="html">HTML</label><br>
  <input type="checkbox" id="design" name="course" value="design">
  <label for="design">Graphic Design</label>
</form>

</body>
</html>
Try it Yourself »

<select> with <option> items makes a dropdown list. <textarea> makes a box for longer, multi-line text.

Example

html
<!DOCTYPE html>
<html>
<body>

<form>
  <label for="course">Course:</label>
  <select id="course" name="course">
    <option value="web">Web Development</option>
    <option value="seo">SEO</option>
    <option value="ai">AI</option>
  </select>
  <br><br>
  <label for="msg">Message:</label><br>
  <textarea id="msg" name="msg" rows="4" cols="30">Please call me after 5 pm.</textarea>
</form>

</body>
</html>
Try it Yourself »

The Submit Button

A <button type="submit"> (or <input type="submit">) sends the form. Here is a complete contact form:

Example

html
<!DOCTYPE html>
<html>
<body>

<h2>Contact Us</h2>
<form action="/contact" method="post">
  <label for="name">Name:</label><br>
  <input type="text" id="name" name="name" required><br><br>
  <label for="phone">Phone:</label><br>
  <input type="tel" id="phone" name="phone"><br><br>
  <button type="submit">Send</button>
</form>

</body>
</html>
Try it Yourself »

The required attribute stops the form from being sent while the name is empty. Try it.

get or post?

MethodHow data travelsUse for
getAdded to the URL, like ?q=htmlSearches and filters
postHidden inside the requestSign-ups, logins, messages

Exercise

Exercise

Several radio buttons should let the user pick only one option. What must they share?

Learn HTML with a teacher

Join the Web Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp