A form is how a web page collects information from a visitor: a login, a registration, a search or a feedback message. The form gathers the data and sends it to a server for processing.

The Form Element

The <form> element wraps all the controls of a form. Two attributes decide what happens on submission. The action attribute holds the address of the server program that receives the data. The method attribute chooses how the data travels, either GET or POST.

Definition: Form. A section of a page that collects user data and sends it to a server.

With GET, the data is added to the address and is visible in the address bar. It suits searches, because the result page can be bookmarked. With POST, the data is sent inside the request and is not shown in the address. It suits logins and any data that changes something on the server.

Input Controls

The <input> element creates most controls. Its type attribute decides which control appears. Each control that sends data must have a name attribute, because the server reads the values by name. A <label> element explains a control to the user, and when its for attribute matches the control's id, clicking the label focuses the control.

Definition: Input control. A form element, such as a text box or button, that lets the user enter or choose data.

Table 1.6: Common input types in a basic form

Type Control Typical use
text One-line text box Name, city
password Box that hides characters Passwords
submit Button that sends the form Login, Save
reset Button that clears the form Start again

Buttons

A submit button sends the form, and a reset button returns all controls to their first values. The <button> element does the same jobs, and it can hold text, images or other elements inside it. A button with type="button" does nothing by itself, and is used with JavaScript.

Example 1.6: A simple login form

<form action="/login" method="post">
  <label for="u">Username</label><br>
  <input type="text" id="u" name="user" placeholder="Enter name"><br>
  <label for="p">Password</label><br>
  <input type="password" id="p" name="pass"><br><br>
  <input type="submit" value="Login">
  <input type="reset" value="Clear">
</form>

Output in the browser:

The server will receive two pairs: user with the typed name and pass with the hidden password.

Common mistakes

  • Forgetting the name attribute, so the control sends no data.
  • Using method GET for a password, which places it in the address bar.
  • Leaving out labels, which makes the form hard to use with a screen reader.
  • Putting controls outside the form element, so that they are not submitted.

Key takeaways

  • A form groups controls, and its action and method decide where and how data is sent.
  • Every control that sends data needs a name attribute.
  • Use POST for passwords and for data that changes something on the server.
  • Labels linked by for and id make forms easier to use.

Practice questions

  1. What does the action attribute of a form hold?
  2. Which input type hides the characters typed?
  3. Why is POST better than GET for a login?

Answers to the practice questions

  1. The address of the server program that receives the data.
  2. The password type.
  3. POST does not show the data in the address bar.

Sources

  • Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit I