chapter-IV(forms)
chapter-IV(forms)
chapter-IV(forms)
Forms
An HTML form is a section of a document which contains controls such as text fields,
password fields, checkboxes, radio buttons, submit button, menus etc. An HTML
form facilitates the user to enter data that is to be sent to the server for processing such
as name, email address, password, phone number, etc.
Definition
Forms are required to take input from the user who visits the website. This form is used
basically for the registration process, logging into your profile on a website or to create
your profile on a website, etc … The information that is collected from the form is -1.
Name 2. Email Addresses etc. Now the form will take input from the form and post that
data in backend applications (like PHP). So the backend application will process the data
which is received by them. There are various form elements that we can use like text
fields, text area, drop-down list, select, checkboxes, radio, etc.
The <input> tag specifies an input field where the user can enter data.
The <input> element is the most important form element.
The <textarea> tag defines a multi-line text input control. The <textarea> element
is often used in a form, to collect user inputs like comments or reviews .
The <button> tag defines a clickable button. Inside a <button> element you can put
text (and tags like <i>, <b>, <strong>, <br>, <img>, etc.). That is not possible with
a button created with the <input> element.
The <select> element is used to create a drop-down list. The <select> element is
most often used in a form, to collect user input.
The <option> tag defines an option in a select list. <option> elements go inside
a <select>, <optgroup>, or <datalist> element.
The <label> tag defines a label for several elements.
The <output> tag is used to represent the result of a calculation (like one performed
by a script).
The <button> tag is used to create a clickable button within HTML form on your
webpage. You can put content like text or image within the <button>.
Attributes:
Form tag
The size attribute specifies the number of visible options in a drop-down list. If the
value of the size attribute is greater than 1, but lower than the total number of
options in the list, the browser will add a scroll bar to indicate that there are more
options to view.
Examples:
The <textarea> tag defines a multi-line text input control. A text area can hold an
unlimited number of characters, and the text renders in a fixed-width font (usually
Courier). The size of a text area is specified by the <cols> and <rows> attributes.
The name attribute is needed to reference the form data after the form is submitted
(if you omit the name attribute, no data from the text area will be submitted).
The id attribute is needed to associate the text area with a label.
Examples:
<form action="/formdemo.php">
<label for="bengaltiger">about bengal tiger</label>
<textarea id="bengaltiger" name="bengaltiger" rows="10" cols="300">
The Bengal tiger is a tiger from a specific population
of the Panthera tigris tigris
subspecies that is native to the Indian subcontinent.
It is threatened by poaching,
loss, and fragmentation of
habitat, and was estimated at comprising fewer
than 2,500 wild individuals by 2011. None of
the Tiger Conservation Landscapes within its
range is considered large enough to support an effective
population of more than 250 adult individuals.
India's tiger population was estimated at 1,706–1,909
individuals in 2010. By 2018, the population
had increased to an estimated 2,603–3,346 individuals.
Around 300–500 tigers are estimated in Bangladesh,
220–274 tigers in Nepal and 103 tigers in Bhutan.
</textarea>
<br><br>
<input type="submit" value="Submit">
</form>
<p>Click the "Submit" button and the form-data will be sent to a page on the
server called "formdemo.php”. </p>
</body>
</html>
Output:
The method attribute specifies how to send form-data (the form-data is sent to the
page specified in the action attribute). The form-data can be sent as URL variables
(with method="get") or as HTTP post transaction (with method="post").
The target attribute specifies where to open the linked document. Blank attribute
opens the linked document in a new window.
Examples:
<form action="/action_page.php"
method="post" target="_blank">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname">
<br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname">
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Output:
Examples:
<! DOCTYPE html>
<html>
<head>
<title> Checkbox Demo </title>
</head>
<body>
<form action="/action_page.php">
<input type="checkbox" id="fruit1" name="fruit1" value="Banana">
<label for="fruit1"> Banana</label><br>
<input type="checkbox" id="fruit2" name="fruit2" value="Mango">
<label for="fruit2">Mango</label><br>
<input type="checkbox" id="fruit3" name="fruit3" value="Apple">
<label for="fruit3">Apple </label>
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Output:
Hidden attribute
The <input type="hidden"> defines a hidden input field. A hidden field let web
developers include data that cannot be seen or modified by users when a form is
submitted. A hidden field often stores what database record that needs to be updated
when the form is submitted.
Examples:
</body>
</html>
Output:
Image
The <input type="image"> defines an image as a submit button. The path to the
image is specified in the src attribute.
<body>
<form action="/image_page.php">
<label for="fname">First name: </label>
<input type="text" id="fname" name="fname">
<br><br>
<label for="lname">Last name: </label>
<input type="text" id="lname" name="lname">
<br><br>
<input type="image" src="Murti.png"
alt="Submit" width="40" height="40">
</form>
<p> The input type="image" sends the X and Y coordinates
of the click that activated the image button. </p>
</body>
</html>
Output:
The <input type="radio"> defines a radio button. Radio buttons are normally
presented in radio groups (a collection of radio buttons describing a set of related
options). Only one radio button in a group can be selected at the same time. The
radio group must have share the same name (the value of the name attribute) to be
treated as a group. Once the radio group is created, selecting any radio button in
that group automatically deselects any other selected radio button in the same
group. You can have as many radio groups on a page as you want, as long as each
group has its own name.
The <input type="reset"> defines a reset button which resets all form values to its
initial values. Avoid reset buttons in your forms! It is frustrating for users if they
click them by mistake. The <input type="submit”> is used to submit the form.
Syntax:
Example:
Output:
Q.N. Write HTML Code to create given form.
Solution:
Gender:
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label>
<label for="female">Female</label>
<input type="radio" id="other" name="gender" value="other">
</select>
<label for = “district” >District:</label>
<select id = “district” name = “district” size="1">
<option value = “Kathmandu” >Kathmandu</option>
Comment:
<textarea name="Contact-Message" rows="6″ cols="20″>
</textarea></br></br>