Class 10Computer ScienceFull chapter

HTML: Forms

A form is how a web page collects information and sends it to a server. Learn to write the form tag with action and method, choose the right input type, use name, value and checked correctly, and produce a complete registration form in the exam.

What a Form Is and How Its Data Reaches the Server

Quick answer A form collects information in the browser and sends it as name=value pairs to a program on the server. HTML designs the form; a server-side script processes it.

A form is the part of a web page that collects information from the person using it and sends that information to a program running on a web server. Every time you search for a train on IRCTC, log in to a school portal, apply for an examination or pay a fee through UPI, you are filling in a form.

To follow where the data goes, recall the client-server model. The client is the browser on your computer or phone. The server is the computer that stores the website and answers requests for it. The two talk using HTTP (HyperText Transfer Protocol), the set of rules for transferring web pages, or its secure version HTTPS. The address you type, such as www.example.com/register.html, is a URL (Uniform Resource Locator) — the complete address of a resource on the web. A school website and IRCTC differ only in what they store; the mechanism is identical.

The journey of form data has four steps, and it is worth being able to state them in order:

  1. The browser displays the page containing the form. The boxes, buttons and lists inside it are called controls (also called form elements).
  2. The user types or selects values and clicks the submit button.
  3. The browser gathers every named control into a set of name=value pairs and sends them to the address written in the form's action attribute.
  4. A server-side script at that address — a program written in a language such as PHP or Python — reads the values, stores them in a database or file, and sends back a reply page such as Registration successful.

The single most important idea in this chapter is the name=value pair. The name attribute that you write in the HTML supplies the name; whatever the user typed or ticked supplies the value. If a student types Anjali into a box written as <input type="text" name="sname">, the browser sends sname=Anjali. Two or more pairs are joined by an ampersand:

sname=Anjali&cls=10&city=Delhi

Now the limitation you must be able to state. HTML only designs the form and sends the data. It cannot process anything. HTML has no way to add marks, compare a password with a stored one, or save a record. That work belongs to the server-side script. A form whose action points to nothing therefore collects data that is simply thrown away when the page is left. A safe one-line answer is: HTML is used only to design the form; the data entered is processed by a program on the server.

There is a small amount of checking the browser itself can do before it contacts the server — refusing to submit an empty box marked required, for example. This is called client-side validation. It is fast and saves a needless trip to the server, but how much of it happens depends on the browser and its version, and a determined user can get around it. Real websites therefore check the same data again on the server. For your paper, remember the pair of terms: client-side validation happens in the browser, server-side validation happens on the server, and serious sites do both.

Finally, note the difference between a form and a form control. The form is the container, written with the <form> tag. The controls are the individual items inside it — text boxes, radio buttons, checkboxes, drop-down lists, the submit button. Questions that ask you to name any four form controls want the controls, not the container.

Form container &lt;form&gt; ... &lt;/form&gt; container · Every control that must be submitted goes inside it.
Data format sent name=value pair · The name comes from the HTML, the value from the user.
Pair separator &amp; symbol · cls=10&amp;city=Delhi sends two pairs.
Protocol HTTP / HTTPS rules · HyperText Transfer Protocol; HTTPS is the encrypted version.
Processed by Server-side script program · PHP, Python and similar languages; never HTML itself.
Remember
  • A form is the part of a web page that collects data from the user and sends it to a program on the web server.
  • The items inside a form &mdash; boxes, buttons, lists &mdash; are called controls or form elements.
  • Data travels as name=value pairs, several pairs joined by an ampersand.
  • HTML only designs the form and sends the data; a server-side script processes and stores it.
  • Client-side validation happens in the browser, server-side validation on the server; reliable sites do both.
  • HTTP is the protocol used to transfer web pages; a URL is the full address of a resource.

The Form Tag: action, method, and GET versus POST

Quick answer action names the program that will receive the data and method decides how it travels. GET puts it in the URL, POST puts it in the request body.

Every control that is to be submitted must sit inside a <form> element. It is a container element: it has an opening tag <form> and a closing tag </form>, and everything between them belongs to that form. Its two attributes that CBSE tests are action and method.

<form action="register.php" method="post">
  <input type="text" name="sname">
  <input type="submit" value="Send">
</form>

action gives the URL of the program that will receive and process the data. It may be a full address such as action="https://www.example.com/register.php" or a relative one such as action="register.php" when the script sits in the same folder. If action is left out, browsers generally send the data back to the same page's address, which is rarely what a student intends — so always write it.

method states how the data travels. It takes one of two values, get or post. The value is not case sensitive, so method="GET" and method="get" behave the same. If method is omitted, GET is used, because get is the default value of the method attribute laid down in the HTML standard.

How GET sends data. The browser takes the name=value pairs, joins them with ampersands and attaches them to the URL after a question mark. The part after the question mark is called the query string:

search.php?city=Delhi&cls=10

Because the data is part of the address, it is visible in the address bar, it is stored in the browser's history, and the page can be bookmarked and reopened with the same values. The length of a URL is limited, so only a small amount of data can be sent this way. The exact limit is set by the browser and the server, not by HTML, so do not quote a number in your answer — write limited length.

How POST sends data. The pairs are placed inside the body of the HTTP request instead of in the address. Nothing appears in the address bar, the page cannot be usefully bookmarked, and the amount of data is not limited by the length of a URL, which is why file uploads always use POST. A server may still refuse an upload larger than a maximum size it has been configured with, so "no limit" means no limit imposed by the address.

When each is used. Use GET when the request only fetches or searches for something and repeating it does no harm — searching for trains between two stations, filtering a list of books in a school library catalogue. Use POST when the request changes something on the server or carries private data — creating an account, logging in, submitting an examination form, making a UPI payment, uploading a photograph.

One correction worth carrying into the exam: POST is not encrypted. It only keeps the values out of the address bar. Anything travelling over plain HTTP can be read on the way, whichever method is used. It is HTTPS that encrypts the data. So the correct sentence is POST is more private than GET because the data is not shown in the URL, not POST is secure.

A good difference-between answer gives at least four contrasted points: where the data is placed, whether it is visible in the address bar, how much data can be sent, and a typical use of each. Add bookmarking or history if a fifth point is wanted.

Form opening tag &lt;form action="file.php" method="post"&gt; syntax · Both attributes should always be written.
Default method get &mdash; · Used automatically when method is left out.
Query string marker ? symbol · search.php?city=Delhi&amp;cls=10 &mdash; only with GET.
GET data location In the URL &mdash; · Visible, bookmarkable, kept in browser history.
POST data location In the request body &mdash; · Not shown in the address bar; required for file upload.
Remember
  • action gives the URL of the server-side program that will receive and process the data.
  • method takes the value get or post; if it is omitted, GET is used.
  • GET attaches data to the URL after a question mark as a query string, so it is visible and limited in length.
  • POST sends data in the body of the request, so it is not shown in the address bar and is not limited by the length of a URL.
  • Use GET for searching or fetching, POST for logging in, registering, paying or uploading.
  • POST is more private than GET but is not encrypted; HTTPS is what encrypts the data.

The Input Tag and Its type Values

Quick answer One empty tag, many controls. The type attribute decides whether you get a text box, a password box, a radio button, a checkbox, a button or a date picker.

The <input> tag creates most of the controls on a form. It is an empty element — it has no closing tag, and nothing is written between tags because there is no second tag. Its type attribute decides which control appears; its name attribute decides the label under which the data is sent. If type is left out, browsers generally treat the control as a text box, but you should always write it.

These are the type values in the syllabus.

  • type="text" — a single-line box for ordinary text such as a name or a city.
  • type="password" — the same box, except the characters are masked as they are typed. Whether they show as dots or asterisks depends on the browser. Masking hides the typing from someone looking over the shoulder; it does not encrypt anything.
  • type="radio" — a small round button. Radio buttons that share the same name form one group, and only one member of a group can be selected. Use it for gender, or for a single stream choice.
  • type="checkbox" — a small square box that is independently ticked or cleared. Several checkboxes may be ticked at once, so it is used for hobbies or optional subjects.
  • type="submit" — the button that actually sends the form to the address in action. A form without one cannot normally be submitted by clicking.
  • type="reset" — a button that returns every control to its original value. Note carefully: reset restores the values that were written in the HTML, it does not empty a box that had a value to start with.
  • type="button" — a plain push button with a caption. By itself it does nothing at all; it is meant to be attached to a script.
  • type="email" — a text box meant for an e-mail address. Browsers that support it may refuse to submit an entry with no @ sign; browsers that do not support it simply show an ordinary text box, which is a safe fallback.
  • type="number" — a box for a numeric entry, often shown with tiny up and down arrows. min and max may be added to fix a range, for example a roll number from 1 to 60.
  • type="date" — a control for choosing a date. How the calendar looks, and the order in which the day and month are displayed, differs between browsers and regional settings, but the value sent to the server is written as YYYY-MM-DD.
  • type="file" — shows a file-choosing button — its caption reads Choose file, Browse or something similar depending on the browser — so the user can pick a file to upload, such as a passport photograph. For an upload to actually work the form must use method="post" and carry enctype="multipart/form-data".
  • type="hidden" — nothing is displayed on the page, yet the name and value are sent along with the rest. It is used to carry a fixed piece of information such as a school code or a form number. It is hidden from the page, not secret — anyone can read it in the page source.
<input type="text"     name="sname">
<input type="password" name="pwd">
<input type="radio"    name="gender" value="Male">
<input type="checkbox" name="hobby"  value="Music">
<input type="email"    name="mail">
<input type="number"   name="roll">
<input type="date"     name="dob">
<input type="file"     name="photo">
<input type="hidden"   name="code" value="SCH-2026">
<input type="submit"   value="Register">
<input type="reset"    value="Clear">

Two distinctions are asked again and again. First, radio versus checkbox: radio is one-from-many within a shared name, checkbox is many-from-many and each one is independent. Second, submit versus reset versus button: submit sends the data, reset restores the starting values, and a plain button does nothing on its own. For submit, reset and button the value attribute is not data at all — it is the caption printed on the button.

Text box &lt;input type="text" name="sname"&gt; control · Single line; the default type if type is missing.
Radio group &lt;input type="radio" name="gender" value="Male"&gt; control · Same name for every button of one group.
Checkbox &lt;input type="checkbox" name="hobby" value="Music"&gt; control · Any number may be ticked at the same time.
Buttons type="submit" / "reset" / "button" control · value supplies the caption printed on the button.
Date value sent YYYY-MM-DD format · The calendar shown varies by browser and region.
Remember
  • &lt;input&gt; is an empty element &mdash; it has no closing tag; type decides which control appears.
  • Radio buttons sharing one name allow only one choice; checkboxes are independent and allow many.
  • submit sends the form, reset restores the original values, and type="button" does nothing on its own.
  • password only masks the characters on screen; it does not encrypt them.
  • hidden is not displayed but its name and value are still submitted, and it is readable in the page source.
  • A file upload needs method="post" together with enctype="multipart/form-data".

name, value and the Attributes That Control a Control

Quick answer Why name is essential, the three jobs of value, the size versus maxlength distinction, and the difference between readonly and disabled.

Attributes are what turn a bare control into a useful one. Learn them by what they do to the control and to the data.

name — the essential one. The browser sends only those controls that have a name. A text box written as <input type="text"> appears on the page, accepts typing and then contributes nothing to the submitted data, because there is no label to send it under. The server-side script also looks the data up by that name, so a spelling mismatch between the HTML and the script loses the value. If a question asks why is the name attribute essential, answer in two parts: it identifies the control so its data can be sent, and it is the key the server uses to read the value.

value does three different jobs depending on the control, and mixing them up is a common error:

  • On a text, password, email, number or date box, value is the initial content shown in the box, which the user may edit.
  • On a radio button or checkbox, value is the data that will be sent if that item is selected. Without it the browser sends a default such as on, which is useless to the script, so always supply it.
  • On submit, reset and button, value is the caption printed on the button.

placeholder puts faint hint text inside an empty box, such as placeholder="you@example.com". It vanishes the moment the user types. It is a hint only — it is not sent to the server and it is not a substitute for a proper label.

size and maxlength are the pair examiners love. size sets the visible width of the box, measured roughly in characters; the user may keep typing past the end and the text simply scrolls. maxlength sets the maximum number of characters that may be typed at all; once the limit is reached the box refuses further input. So size="10" maxlength="30" is a short box that still accepts thirty characters, and this is perfectly legal.

checked is written on a radio button or checkbox to make it already selected when the page loads. It needs no value: checked alone is enough. Within one radio group only one button should carry it.

required tells the browser not to submit the form while that control is empty; the browser shows its own message. Support and the wording of the message vary between browsers, and older ones ignore the attribute completely — another reason the server must re-check.

readonly and disabled look similar on screen and differ where it matters:

  • readonly — the user can see, select and copy the text but cannot change it, and the value is still submitted. Use it for a figure fixed by the school, such as an examination fee of ₹500.
  • disabled — the control is greyed out, cannot be used or focused, and its value is not submitted at all.

Grouping radio buttons. A radio group is created purely by giving the buttons the same name and different values:

Gender:
<input type="radio" name="gender" value="Male" checked> Male
<input type="radio" name="gender" value="Female"> Female
<input type="radio" name="gender" value="Other"> Other

Hobbies:
<input type="checkbox" name="hobby" value="Music"> Music
<input type="checkbox" name="hobby" value="Sports" checked> Sports

If the three radio buttons above were given three different names, the browser would treat each as a group of one and the user could switch on all three together — the classic find the error question. Only one pair is sent from a radio group, here gender=Male; checkboxes send one pair for each box that is ticked.

Essential attribute name="..." &mdash; · No name, no data &mdash; the control is simply ignored on submit.
Visible width vs limit size="10" maxlength="30" characters · A short box that still accepts thirty characters.
Pre-selected item checked &mdash; · Written alone; only one per radio group.
Submitted or not readonly &#8594; sent , disabled &#8594; not sent rule · The distinction most often asked in the paper.
Hint text placeholder="you@example.com" &mdash; · Disappears when typing starts and is never submitted.
Remember
  • Only controls that have a name attribute are submitted; a control without one sends nothing.
  • value means the initial text in a box, the data sent by a ticked radio or checkbox, or the caption on a button.
  • size sets the visible width of a box; maxlength sets the greatest number of characters that may be typed.
  • checked pre-selects a radio button or checkbox; required stops submission while the control is empty.
  • readonly cannot be edited but is still submitted; disabled cannot be used and is not submitted.
  • Radio buttons form one group only when they share the same name and carry different values.

Textarea, Select with Option, and the Label Tag

Quick answer Multi-line text with rows and cols, drop-down lists built from select and option with the selected attribute, and labels joined to controls through for and id.

Three controls are written with tags of their own rather than with <input>.

The textarea. <textarea> creates a multi-line box for longer text such as a postal address, a complaint or a short answer. Unlike <input> it is a container element with a closing tag, and whatever is written between the tags becomes the default text shown in the box.

<textarea name="address" rows="4" cols="40">Type your full address here</textarea>
  • rows — the number of lines of text visible at a time.
  • cols — the number of characters visible across one line.

Both control only the visible size of the box. The user may type far more than rows × cols characters; the box simply scrolls. If the amount of text must actually be limited, add maxlength. Also note the trap: writing value="..." on a textarea does nothing — the default text goes between the tags. And, as always, no name means no data.

The drop-down list. A list is built from two tags working together. <select> is the list itself and carries the name; each choice inside it is an <option>.

<select name="city">
  <option value="DEL">Delhi</option>
  <option value="MUM">Mumbai</option>
  <option value="CHE" selected>Chennai</option>
</select>

Points worth remembering. The name belongs on <select>, never on <option>. The value attribute of an option is what is sent to the server; if it is omitted, the text written between <option> and </option> is sent instead. The selected attribute marks the option that is shown when the page loads — the example above submits city=CHE if the user changes nothing. Without selected, browsers generally display the first option. Two optional extras are worth one line each: size="4" turns the drop-down into a scrolling list box showing four items, and multiple allows more than one option to be chosen.

The label. <label> attaches a piece of text to a particular control. Its for attribute must contain the id of that control — not the name.

<label for="sname">Full name:</label>
<input type="text" id="sname" name="sname" size="30">

When they are linked this way, clicking the words Full name: places the cursor in the box. The gain is largest with radio buttons and checkboxes, whose clickable circle or square is tiny, and on a phone screen where accurate tapping is hard:

<input type="radio" id="g1" name="gender" value="Male">
<label for="g1">Male</label>

This forces a distinction you must be able to state: id versus name. The id identifies the element within the page and must be unique on that page; it is what for points to. The name identifies the data for the server; radio buttons in one group deliberately share it. A control that needs both simply carries both, and they may be given the same spelling, as id="sname" name="sname" above, without any conflict — but writing for="sname" and then giving the input only a name and no id leaves the label joined to nothing.

Multi-line box &lt;textarea name="address" rows="4" cols="40"&gt;&lt;/textarea&gt; control · rows = lines visible, cols = characters across.
Drop-down list &lt;select name="city"&gt; &lt;option&gt;...&lt;/option&gt; &lt;/select&gt; control · name on select, never on option.
Default choice &lt;option value="CHE" selected&gt;Chennai&lt;/option&gt; &mdash; · Submits city=CHE if nothing is changed.
Label link &lt;label for="sname"&gt; &#8596; id="sname" rule · for must match id; clicking the label activates the control.
List box size="4" , multiple &mdash; · Shows four items at a time; multiple allows more than one choice.
Remember
  • &lt;textarea&gt; is a container tag; the text between the tags is the default content, and rows and cols set only the visible size.
  • In a drop-down list the name belongs to &lt;select&gt;, while each choice is written as an &lt;option&gt;.
  • An option's value is what is sent; if value is omitted the text between the option tags is sent.
  • selected marks the option displayed when the page loads; otherwise browsers generally show the first option.
  • &lt;label&gt; is joined to a control by matching its for attribute with the control's id, not its name.
  • id identifies an element within the page and must be unique; name identifies the data for the server and may be shared.

Fieldset, Legend and a Complete Registration Form

Quick answer Grouping related controls in a captioned box, a full worked registration form using every tag in the syllabus, and exactly which pairs it sends.

<fieldset> draws a box around a set of related controls so that a long form reads as a few small sections instead of one crowded list. <legend>, written as the first thing inside a fieldset, supplies its caption, which browsers generally draw sitting on the top border of the box. Both are container elements, and neither sends any data — they are for organising the form only.

<fieldset>
  <legend>Personal Details</legend>
  <input type="text" name="sname">
</fieldset>

Here is a complete registration form for a school website, using every item in the syllabus. Read it once as a whole, then read the notes below it.

<form action="register.php" method="post" enctype="multipart/form-data">

  <fieldset>
    <legend>Personal Details</legend>

    <label for="sname">Full name:</label>
    <input type="text" id="sname" name="sname" size="30" maxlength="40" required>
    <br>

    <label for="mail">E-mail:</label>
    <input type="email" id="mail" name="email" size="30"
           placeholder="you@example.com" required>
    <br>

    <label for="pwd">Password:</label>
    <input type="password" id="pwd" name="pwd" size="20" maxlength="12" required>
    <br>

    <label for="dob">Date of birth:</label>
    <input type="date" id="dob" name="dob">
    <br>

    Gender:
    <input type="radio" id="g1" name="gender" value="Male" checked>
    <label for="g1">Male</label>
    <input type="radio" id="g2" name="gender" value="Female">
    <label for="g2">Female</label>
  </fieldset>

  <fieldset>
    <legend>Academic Details</legend>

    <label for="cls">Class:</label>
    <select id="cls" name="cls">
      <option value="9">Class 9</option>
      <option value="10" selected>Class 10</option>
      <option value="11">Class 11</option>
    </select>
    <br>

    Optional subjects:
    <input type="checkbox" id="s1" name="subject" value="AI">
    <label for="s1">Artificial Intelligence</label>
    <input type="checkbox" id="s2" name="subject" value="IT">
    <label for="s2">Information Technology</label>
    <br>

    <label for="roll">Roll number:</label>
    <input type="number" id="roll" name="roll" min="1" max="60">
    <br>

    <label for="pic">Passport photo:</label>
    <input type="file" id="pic" name="photo">
    <br>

    <label for="addr">Address:</label><br>
    <textarea id="addr" name="address" rows="4" cols="40"></textarea>
  </fieldset>

  <label for="fee">Fee (Rs.):</label>
  <input type="text" id="fee" name="fee" value="500" readonly>

  <input type="hidden" name="schoolcode" value="SCH-2026">

  <input type="submit" value="Register">
  <input type="reset" value="Clear form">
</form>

What is actually sent. Suppose a student fills the form, ticks only Artificial Intelligence, leaves the class as Class 10 and clicks Register. The browser builds pairs such as sname=Anjali, email=..., pwd=..., dob=2010-06-14, gender=Male, cls=10, subject=AI, roll=27, address=..., fee=500 and schoolcode=SCH-2026, plus the chosen file, and posts them all to register.php. The fee box is sent although the user could not edit it, because it is readonly; had it been disabled it would not have been sent. The unticked Information Technology checkbox sends nothing. The fieldsets, legends and labels send nothing.

A checklist for writing a form in the examination. Open with <form> and give it both action and method. Give every control a name. Give radio buttons in one group the same name and different values. Give every checkbox and radio a value. Close <textarea>, <select>, <option>, <fieldset>, <legend>, <label> and </form>. Finish with a submit button. If the question mentions uploading a file, write method="post". Answers to these questions are judged on correct tags, attributes and closing tags, so neat, correctly closed code matters more than pretty layout.

Grouping box &lt;fieldset&gt; ... &lt;/fieldset&gt; container · Draws a border around related controls.
Box caption &lt;legend&gt;Personal Details&lt;/legend&gt; container · Written as the first element inside the fieldset.
Upload form header method="post" enctype="multipart/form-data" syntax · Both are needed for type="file" to work.
Submit and reset &lt;input type="submit" value="Register"&gt; control · value is the caption; reset restores the starting values.
Remember
  • &lt;fieldset&gt; groups related controls inside a box and &lt;legend&gt; gives that box its caption.
  • Neither fieldset nor legend nor label sends any data; they only organise the form.
  • A readonly control is submitted; an unticked checkbox and a disabled control are not.
  • A file upload form must use method="post" with enctype="multipart/form-data".
  • In write-the-code questions, correct tags, attributes and closing tags are what matter.
  • Always end the form with a submit button, and close the form with &lt;/form&gt;.

Quick reference

Every term, tag and rule from this chapter in one place — screenshot it before your exam.

&lt;form&gt; ... &lt;/form&gt;
Form containercontainer
name=value
Data format sentpair
&amp;
Pair separatorsymbol
HTTP / HTTPS
Protocolrules
Server-side script
Processed byprogram
&lt;form action="file.php" method="post"&gt;
Form opening tagsyntax
get
Default method&mdash;
?
Query string markersymbol
In the URL
GET data location&mdash;
In the request body
POST data location&mdash;
&lt;input type="text" name="sname"&gt;
Text boxcontrol
&lt;input type="radio" name="gender" value="Male"&gt;
Radio groupcontrol
&lt;input type="checkbox" name="hobby" value="Music"&gt;
Checkboxcontrol
type="submit" / "reset" / "button"
Buttonscontrol
YYYY-MM-DD
Date value sentformat
name="..."
Essential attribute&mdash;
size="10" maxlength="30"
Visible width vs limitcharacters
checked
Pre-selected item&mdash;
readonly &#8594; sent , disabled &#8594; not sent
Submitted or notrule
placeholder="you@example.com"
Hint text&mdash;
&lt;textarea name="address" rows="4" cols="40"&gt;&lt;/textarea&gt;
Multi-line boxcontrol
&lt;select name="city"&gt; &lt;option&gt;...&lt;/option&gt; &lt;/select&gt;
Drop-down listcontrol
&lt;option value="CHE" selected&gt;Chennai&lt;/option&gt;
Default choice&mdash;
&lt;label for="sname"&gt; &#8596; id="sname"
Label linkrule
size="4" , multiple
List box&mdash;
&lt;fieldset&gt; ... &lt;/fieldset&gt;
Grouping boxcontainer
&lt;legend&gt;Personal Details&lt;/legend&gt;
Box captioncontainer
method="post" enctype="multipart/form-data"
Upload form headersyntax
&lt;input type="submit" value="Register"&gt;
Submit and resetcontrol

Test yourself

Tap an answer to check it instantly — you'll see why it's right, and what to revise if it isn't.

0 correct · 0/12 answered
Q1 The form tag easy

Which attribute of the &lt;form&gt; tag gives the address of the program that will receive the submitted data?

Q2 GET and POST medium

A form is written as &lt;form action="reply.php"&gt; with no method attribute. Which method will the browser use?

Q3 GET and POST easy

After a form is submitted the address bar shows search.php?city=Delhi&amp;cls=10. What does this tell you?

Q4 Radio buttons medium

Three radio buttons are meant to offer one gender choice, but each has been given a different name. What will happen?

Q5 The name attribute easy

A text box is written as &lt;input type="text" size="25"&gt;. The user types a city into it and submits the form. What reaches the server?

Q6 size and maxlength medium

What is the effect of &lt;input type="text" name="pin" size="6" maxlength="12"&gt;?

Q7 readonly and disabled hard

A fee box is written as &lt;input type="text" name="fee" value="500" disabled&gt;. What does the server receive?

Q8 select and option easy

Which attribute makes one entry of a drop-down list appear as the default when the page loads?

Q9 The label tag hard

For a label to be joined to a text box, the label's for attribute must match which attribute of that box?

Q10 textarea easy

In &lt;textarea name="remark" rows="3" cols="30"&gt;&lt;/textarea&gt;, what do rows and cols decide?

Q11 Input types medium

Which input type is not shown on the page, yet its name and value are still sent when the form is submitted?

Q12 GET and POST hard

Which statement about the POST method is correct?

NCERT solutions & previous-year questions

Step-by-step model answers — tap a question to reveal the full solution.

NCERT questions 8

1 What is an HTML form? Why are forms used on websites?Introduction to forms

An HTML form is the part of a web page that collects information from the user and sends it to a program on the web server for processing. It is written inside the container element <form> ... </form>, and the boxes, buttons and lists placed within it are called controls or form elements.

Forms are used because a web page must sometimes take input rather than only display information. Typical uses are registration and login, searching (for example looking for a train between two stations on IRCTC), submitting an examination or admission form on a school website, giving feedback, and making a payment through UPI or a card.

Remember the limitation: HTML only designs the form and sends the data as name=value pairs to the address given in the action attribute. The data is then read, checked and stored by a server-side script written in a language such as PHP or Python.

2 Differentiate between the GET and POST methods of sending form data. Give one suitable use of each.GET and POST

GET

  • The data is attached to the URL after a question mark, as a query string: search.php?city=Delhi&cls=10.
  • The values are visible in the address bar and are kept in the browser's history; the page can be bookmarked.
  • Only a limited amount of data can be sent, because the length of a URL is limited.
  • It is the default method when method is not written.
  • Use: searching or fetching information, such as looking for trains between two stations.

POST

  • The data is placed in the body of the HTTP request, not in the URL.
  • Nothing appears in the address bar and the page cannot usefully be bookmarked.
  • The amount of data is not limited by the length of a URL, so file uploads use POST; a server may still set its own maximum upload size.
  • It must be written explicitly as method="post".
  • Use: registration, login, submitting an examination form, making a payment.

Note that POST is more private than GET, but it is not encrypted; encryption comes from using HTTPS.

3 Why is the name attribute essential in a form control? What happens if it is omitted?The name attribute

When a form is submitted, the browser builds a set of name=value pairs. The name attribute supplies the name and the user's entry supplies the value, so name is the label under which that control's data travels. The server-side script then reads the value by asking for that same name, which means the spelling in the HTML and in the script must match.

If name is omitted, the control still appears on the page and still accepts input, but it is not submitted at all — its data is silently discarded. For example, <input type="text" size="25"> sends nothing, while <input type="text" name="city" size="25"> sends city=Delhi.

4 Distinguish between a radio button and a checkbox. Write one line of HTML for each.Radio buttons and checkboxes

A radio button allows only one choice from a group. Buttons belong to the same group when they share the same name and are given different values, so selecting one clears the others. It suits a gender or a single stream choice.

A checkbox is independent. Each box can be ticked or cleared on its own and several may be ticked at the same time, so it suits hobbies or optional subjects. Each ticked box sends its own pair; unticked boxes send nothing.

<input type="radio" name="gender" value="Male" checked> Male
<input type="radio" name="gender" value="Female"> Female

<input type="checkbox" name="hobby" value="Music"> Music
<input type="checkbox" name="hobby" value="Sports"> Sports

Both use checked to be selected in advance, and both need a value, otherwise the browser sends a default such as on, which tells the script nothing.

5 Write the HTML code to create a drop-down list of three cities &mdash; Delhi, Mumbai and Chennai &mdash; with Chennai selected by default. Name the list city.select and option
<select name="city">
  <option value="DEL">Delhi</option>
  <option value="MUM">Mumbai</option>
  <option value="CHE" selected>Chennai</option>
</select>

Points to note. The name is written on <select>, never on <option>. The value of an option is what is sent to the server; if it is left out, the text written between <option> and </option> is sent instead. The selected attribute makes Chennai appear when the page loads, so the form submits city=CHE unless the user changes it.

6 Explain the &lt;textarea&gt; tag. What do its rows and cols attributes do?textarea

<textarea> creates a multi-line text box, used for longer entries such as an address, a complaint or a short answer. Unlike <input> it is a container element with a closing tag, and any text written between the opening and closing tags appears in the box as the default content.

<textarea name="address" rows="4" cols="40">Type your full address here</textarea>

rows gives the number of lines of text that are visible at one time and cols gives the number of characters visible across a line. Both fix only the visible size of the box: the user may type more than that and the box simply scrolls. To place a real limit on the amount of text, add maxlength. A value attribute has no effect on a textarea, and without a name nothing is submitted.

7 What is the purpose of the &lt;fieldset&gt; and &lt;legend&gt; tags?fieldset and legend

<fieldset> groups related controls together and draws a box around them, so that a long form is read as a few small sections instead of one crowded list — for example, one fieldset for personal details and another for academic details.

<legend> is written as the first element inside a fieldset and supplies its caption. Browsers generally draw this caption sitting on the top border of the box, although the exact appearance varies.

<fieldset>
  <legend>Personal Details</legend>
  <input type="text" name="sname">
</fieldset>

Both are container elements and both must be closed. Neither of them sends any data when the form is submitted; they exist only to organise the form.

8 Differentiate between the readonly and disabled attributes.readonly and disabled

readonly — the control is displayed normally and its text can be selected and copied, but it cannot be changed by the user. Its value is submitted with the rest of the form. It suits a figure fixed by the organisation, such as an examination fee of ₹500.

disabled — the control is greyed out, cannot be clicked, focused or changed, and its value is not submitted at all.

<input type="text" name="fee" value="500" readonly>
<input type="text" name="fee" value="500" disabled>

The first line sends fee=500; the second sends nothing. That difference in what reaches the server is the point of the question.

Previous-year board questions 6

Q1 Name the attribute used to select a checkbox or a radio button by default when the page is loaded. 1 mark

checked. It is written on its own, without a value, for example <input type="checkbox" name="hobby" value="Music" checked>. Within one radio group only one button should carry it.

Q2 What is the difference between the size and maxlength attributes of a text box? Support your answer with an example. 2 marks

size fixes the visible width of the box, measured roughly in characters. maxlength fixes the greatest number of characters that may be typed into it; once that limit is reached the box accepts nothing more.

<input type="text" name="pin" size="6" maxlength="12">

Here the box looks about six characters wide, yet the user may type up to twelve characters, the text scrolling inside the narrow box. So size affects appearance only, while maxlength affects the data.

Q3 Write HTML code to create three radio buttons for Science, Commerce and Humanities, with Science selected by default. Explain why they must be given the same name. 3 marks
Stream:
<input type="radio" name="stream" value="Science" checked> Science
<input type="radio" name="stream" value="Commerce"> Commerce
<input type="radio" name="stream" value="Humanities"> Humanities

The three buttons must share the name stream because a shared name is what makes the browser treat them as one group, and only one member of a group can be selected at a time — choosing one automatically clears the others. If each button were given a different name, the browser would treat each as a group of one and the user could switch on all three together.

The values differ so that the server can tell which stream was chosen; this form submits stream=Science unless the user changes it.

Q4 Write the HTML code for a registration form of a school website that sends its data to admit.php using the POST method. It must contain: a text box for the student's name, a password box, an e-mail box, a date of birth field, radio buttons for gender, a drop-down list for class, a multi-line address box, and submit and reset buttons. 5 marks
<form action="admit.php" method="post">
  <fieldset>
    <legend>Admission Form</legend>

    <label for="sname">Name:</label>
    <input type="text" id="sname" name="sname" size="30" maxlength="40" required>
    <br>

    <label for="pwd">Password:</label>
    <input type="password" id="pwd" name="pwd" size="20" maxlength="12">
    <br>

    <label for="mail">E-mail:</label>
    <input type="email" id="mail" name="email" size="30">
    <br>

    <label for="dob">Date of birth:</label>
    <input type="date" id="dob" name="dob">
    <br>

    Gender:
    <input type="radio" name="gender" value="Male" checked> Male
    <input type="radio" name="gender" value="Female"> Female
    <br>

    <label for="cls">Class:</label>
    <select id="cls" name="cls">
      <option value="9">Class 9</option>
      <option value="10" selected>Class 10</option>
    </select>
    <br>

    <label for="addr">Address:</label><br>
    <textarea id="addr" name="address" rows="4" cols="40"></textarea>
    <br>

    <input type="submit" value="Submit">
    <input type="reset" value="Clear">
  </fieldset>
</form>

Such an answer is judged on the correct opening tag with both action and method, a name on every control, the correct type for each field, the shared name on the radio group, a properly closed <textarea> and <select>, and the closing </form>.

Q5 What is the purpose of the &lt;label&gt; tag? Which attribute joins it to a control, and what must that attribute contain? 2 marks

<label> attaches a piece of descriptive text to a particular form control, so that the user knows what to enter. When the two are joined, clicking the label text activates the control — the cursor jumps into the box, or the radio button is selected. This is especially useful for radio buttons and checkboxes, whose clickable area is very small, and on touch screens.

The joining attribute is for, and it must contain the id of the control, not its name.

<label for="sname">Full name:</label>
<input type="text" id="sname" name="sname">
Q6 The following code does not work as intended. Identify any three errors and rewrite the corrected code. 3 marks

Consider the faulty code:

<form action="reg.php">
  <input type="text" size="20">
  <input type="radio" name="m" value="Male"> Male
  <input type="radio" name="f" value="Female"> Female
  <textarea name="address" rows="3" cols="30" value="Address">
  <input type="reset" value="Send">
</form>

Errors. (1) The text box has no name, so its data is never submitted. (2) The two radio buttons have different names, so they do not form one group and both can be selected together. (3) The <textarea> is not closed and uses a value attribute, which has no effect — its default text must be written between the tags. (4) The form is sent by a reset button, which only clears the form; a submit button is needed. A method should also be stated, and post is suitable for a registration form.

<form action="reg.php" method="post">
  <input type="text" name="sname" size="20">
  <input type="radio" name="gender" value="Male"> Male
  <input type="radio" name="gender" value="Female"> Female
  <textarea name="address" rows="3" cols="30">Address</textarea>
  <input type="submit" value="Send">
</form>

Part of Priodemy for School

Interactive Maths & Science — free with every school on Priodemy EduSuite. Explore more chapters and labs on the Priodemy for School hub.

Ask AI