Forms collect information from users, such as login details, registrations and feedback, and send it to a server.

The FORM Tag

The <form> tag contains the input controls. Its action attribute gives the URL of the program that will process the data, and its method attribute gives how the data is sent: GET adds the data to the URL, which is visible and suitable for searches, and POST sends it in the body of the request, which is used for passwords and large data. Each control needs a name attribute, which becomes the key of its value in the submitted data.

Definition: Form. An HTML element that groups input controls for collecting user data and submits that data to a server program given by its action attribute.

The INPUT Tag

<input> is an empty element whose type attribute decides the control. text gives a one-line text box, password hides the characters, email, number, tel, url and date give fields that the browser checks and that show suitable keyboards on phones, radio gives buttons from which one is chosen (those with the same name form a group), checkbox gives boxes of which several may be ticked, file uploads a file, range gives a slider, color a colour picker, hidden a value not shown, and submit and reset give buttons that send or clear the form. Useful attributes are value, placeholder (hint text), required, maxlength, min, max, pattern, readonly and checked.

Labels, TEXTAREA and SELECT

<label for="id"> gives a caption to a control and makes clicking the caption focus the control. <textarea> gives a multi-line text box, with rows and cols attributes, for comments and addresses. <select> gives a drop-down list, with each choice in an <option>; the selected attribute chooses the default, multiple allows several choices, and <optgroup> groups options.

FIELDSET and LEGEND

<fieldset> groups related controls and draws a border round them, and <legend> gives the group a title, such as "Personal Details" and "Course Details". Grouping makes long forms easier to fill in.

Buttons

<button type="submit"> sends the form, type="reset" clears it, and type="button" does nothing by itself and is used with JavaScript.

Table 1.5: Common input types

Type Control Use
text, password Text box Name, password
email, number, date Checked text box Email, age, birth date
radio One of a group Gender, year
checkbox Several choices Hobbies, subjects
submit, reset Buttons Send or clear

Example 1.5: A student registration form

Problem: Create a registration form with name, email, password, gender (radio), course (select), hobbies (checkbox), address (textarea), grouped in fieldsets, with submit and reset buttons.

Solution:

  1. Use <form action="register.php" method="post">.
  2. Group personal details and course details in two <fieldset> elements with <legend>.
  3. Use input types text, email, password, radio and checkbox, with labels and required.
  4. Add <select> for the course, <textarea> for the address, and submit and reset buttons.

Result: A complete, grouped form that checks the required fields before sending.

<form action="register.php" method="post">
  <fieldset>
    <legend>Personal Details</legend>
    <label for="nm">Name:</label>
    <input type="text" id="nm" name="name" required><br>
    <label for="em">Email:</label>
    <input type="email" id="em" name="email" required><br>
    <input type="password" name="pwd" placeholder="Password"
           minlength="6"><br>
    Gender: <input type="radio" name="gender" value="F"> Female
            <input type="radio" name="gender" value="M"> Male
  </fieldset>
  <fieldset>
    <legend>Course Details</legend>
    <select name="course">
      <option>BCA</option><option>BBA</option><option>B.Com</option>
    </select>
    Hobbies: <input type="checkbox" name="hob" value="music"> Music
             <input type="checkbox" name="hob" value="sports"> Sports
    <br><textarea name="address" rows="3" cols="40"></textarea>
  </fieldset>
  <input type="submit" value="Register"> <input type="reset">
</form>

Common mistakes

  • Leaving out the name attribute, so that the value is not sent.
  • Giving radio buttons different names, so that more than one can be selected.
  • Sending passwords with GET, which shows them in the URL.

Key takeaways

  • <form action method> sends the data of its controls.
  • The type of <input> decides the control.
  • Radio buttons with the same name allow one choice; checkboxes allow many.
  • <textarea>, <select> and <fieldset> with <legend> complete forms.

Practice questions

  1. Distinguish GET and POST.
  2. How do you allow only one choice among options?
  3. What is the use of fieldset?

Answers to the practice questions

  1. GET sends data in the URL; POST sends it in the request body, which is better for passwords and large data.
  2. Use radio buttons with the same name.
  3. It groups related form controls with a border, titled by a legend.

Sources

  • Official BCA syllabus Web Technologies, course code 25BCA402SEC02, Unit I