CHAPTER 31 · HTML

Forms: collect named values and understand what Submit does

A form gives a reader places to enter information and a way to send it. Build a small practice form, explain each attribute and distinguish the visible controls from the program that would receive and save real data.

Download example files (ZIP)

Think of a library request slip

A paper slip has labelled spaces: book title, student name and collection day. A person fills those spaces and hands the slip to the library desk. An HTML form is a similar group of controls on a page. A control is an interactive item such as a text field, checkbox or button.

The form is the slip, not the librarian. HTML can describe the fields and organise a submission. Saving a request or creating a real booking requires a receiving program. Do not call a static confirmation page a working booking service.

The first form, one part at a time

HTML fragment — part of a document
<form action="form-result.html" method="get">
  <p><label for="topic">Practice topic</label></p>
  <p><input type="text" id="topic" name="topic"></p>
  <button type="submit">Try the submission</button>
</form>
PartMeaning
formGroup the controls for this submission
actionGive the address to which the form submits
method="get"Put the submitted values in a URL query
labelSupply the visible name of a control
for="topic"Connect that label to the control with id="topic"
input type="text"Create a single-line text field
name="topic"Supply the field's key in the submitted data
button type="submit"Request submission of this form

Input is void and has no closing tag. Form, label and button have opening and closing tags. Write button types explicitly. Do not nest one form inside another.

Id, name and value do different jobs

Id identifies an element in this document, for example so a label can refer to it. Name identifies its entry in submitted data. Value is the control's current value; a value attribute can provide a starting value for a text input.

If a student types Arrays in the named topic field, the submitted pair is topic=Arrays. Changing only id to lesson does not rename that submitted key. Removing name from this text field leaves it visible but excludes its value from ordinary form submission.

Keep ids unique. Several controls may share a name when the data model calls for repeated values or a radio group, which we will practise later.

Follow the named values
  1. 1Read the label
  2. 2Enter a fictional value
  3. 3Submit a name/value pair
  4. 4Inspect the destination query

The static destination demonstrates navigation. It does not save a record or reserve a place.

Complete program: topic-form.html

Save this program and the following result page beside each other. Use invented practice values. No real registration or data storage is involved.

topic-form.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Try a Named Form Field</title>
</head>
<body>
  <h1>Choose a topic to practise</h1>
  <p>This local exercise demonstrates a submission URL. It does not register a student.</p>
  <form action="form-result.html" method="get">
    <p><label for="topic">Practice topic</label></p>
    <p><input type="text" id="topic" name="topic" value="Headings"></p>
    <button type="submit">Try the submission</button>
  </form>
</body>
</html>
Browser previewDocument title: Try a Named Form Field

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here. Form submission is disabled in this preview. Save the example and its result page locally to test submission. Use fictional practice values.

Expected result: a labelled field starts with Headings and a submit button appears. Activating the label should focus its field. In your saved page, change the text to Lists and submit. The browser should open the result file with a query such as ?topic=Lists.

The embedded lesson preview lets you inspect and edit fields, but submission is disabled there. Save both complete files to test the action in an ordinary browser tab. The final example pack will be offered after the full tutorial is reviewed.

Complete program: form-result.html

form-result.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Local Form Destination</title>
</head>
<body>
  <h1>You opened the local form destination</h1>
  <p>Look at the address bar for the submitted query values.</p>
  <p>This static page does not read, save or validate your submission on a server.</p>
  <p><a href="topic-form.html">Return to the topic form</a></p>
</body>
</html>
Browser previewDocument title: Local Form Destination

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

Expected result: a fixed explanation, not a personalised message. The URL demonstrates the named values; this page does not contain code to display them. That distinction is important: navigation can work even though no application has processed a request.

GET and POST in plain words

GET is suitable for retrieving information, such as a search whose filters can appear in its address. Normal GET form submission places named values in the query. Spaces and special characters are encoded; seeing + or %20 for a space is not the browser deleting your words.

POST sends form data in the request body to a receiving endpoint. An endpoint is an address where a program handles a request. POST alone does not save anything or encrypt the data. Real sensitive submissions need HTTPS and appropriate server handling. Never put passwords into our local GET demonstrations.

A static file server may reject POST because it has no receiving application. Simply changing method to post is not enough to turn form-result.html into a working handler. The usual default method is GET; write it explicitly while learning. If action is omitted, the current document's address is used, which also does not invent a handler.

What gets submitted?

Not every visible control contributes a value. A normal text field needs a name. Disabled controls are excluded. Unchecked radio buttons and checkboxes are excluded. A submit button can contribute its own name/value when it is the submitter. The next chapters let you inspect these rules directly.

An id or visible label does not replace name. Conversely, a name is not a visible label. The form needs both understandable presentation and an appropriate data structure.

Other form settings you will meet

Target controls the destination browsing context; normally keep the current tab. Autocomplete can guide browsers about autofill, but it is not a data-storage service. Enctype describes how a POST body is encoded; file uploads commonly need multipart/form-data and a real upload handler. We will return to uploads with file controls.

Ordinary application actions belong on buttons; navigation belongs on links. A button with type="button" does not submit by itself. A reset button restores initial control values rather than necessarily making everything empty; use it only when that action helps the reader.

Problem and solution: the field is visible but missing from the URL

Problem: a label correctly focuses the input, but submitting does not include the entered topic.

Solution: check the input's name and disabled state. A working label connection proves the id relationship, not submission membership. Restore name="topic" and submit a fictional value again.

Common mistakes and useful tips

  • Treating a static destination as a completed booking system.
  • Using id instead of name for submitted data.
  • Leaving controls without clear visible labels.
  • Nesting forms or omitting button types.
  • Expecting POST to work on a static result file.
  • Using real private data in a GET teaching example.
  • Saying a form saves information simply because a button can be pressed.

Practice questions

  1. What is the difference between a form and its receiving program?
  2. What does action specify?
  3. How do id and name differ?
  4. Where do GET form values normally appear?
  5. Why does changing method to post not make a static page save data?
  6. Save both programs. Submit topic=Lists, then remove the input's name and compare the resulting address. Restore the name afterwards.
Check your practice answers
  1. The form describes controls and submission. A receiving program processes the request and may save data if implemented to do so.
  2. The submission destination address.
  3. Id identifies a document element; name identifies the submitted data key.
  4. In the destination URL's query.
  5. The destination still needs code that accepts and processes POST requests. The method is not a storage feature.
  6. With name present, the query contains topic=Lists. Without that name, the ordinary text field contributes no entry. The result page remains a fixed demonstration either way.

Interview questions

Does HTML alone create a complete registration system?

No. It supplies the form interface and native submission behaviour. Processing, server-side checks and persistence need an appropriate receiving application.

Why can a labelled field still fail to submit its value?

Labels usually connect through id, while ordinary submission requires name and an eligible control state. A disabled or unnamed field can look present without contributing data.

Does POST automatically provide security?

No. POST describes a request method and body handling, not encryption or access control. Use HTTPS and correct receiving-side handling for real services.

Why write an explicit button type?

It makes the intended behaviour clear. An ordinary button associated with a form can submit by default; type="button" avoids an unintended submission when no submission is intended.

Quick revision

  • Forms group labelled controls and describe submission.
  • Id connects document elements; name/value describes submitted entries.
  • GET and POST differ, and neither creates a receiving application.
  • Save both local example files and keep practice values fictional.