HTML Form
HTML Form
HTML Forms are required, when you want to collect some data from the site visitor. For
example, during user registration you would like to collect information such as name, email
address, phone number, etc.
A form will take input from the site visitor and then will post it to a back-end application such
as ASP Script or PHP script etc. The back-end application will perform required processing on
the passed data based on defined business logic inside the application.
There are various form elements available like text fields, textarea fields, drop-down menus,
radio buttons, checkboxes, etc.
The HTML <form> tag is used to create an HTML form and it has following syntax −
<form action = "Script URL" method = "GET|POST">
form elements like input, textarea etc.
</form>
Attributes of <from> tag
Attributes Value Description
action URL Specifies where to send the
form-data when a form is
submitted.
method Get or post Specifies the HTTP method to
use when sending form-data.
enctype • application/x-www-form-urlencoded Specifies how the form-data
• multipart/form-data should be encoded when
submitting it to the server.
Traditionally, the <input> tag is found within the <form> tag and can represent
text fields, checkboxes, radio button, buttons and other inputs, simply by
setting the appropriate type attribute.
disabled Boolean value indicating that the user cannot interact with the
input.
Type Type of the input. It can be one of the following values: button,
checkbox, color, date, email, file, number, password, radio, range,
reset, search, submit, tel, text, time, url etc.
Checkboxes
Checkboxes allows the user to select one or more option from a pre-defined set
of options. It is created using an <input> element whose type attribute has a value
of checkbox.
Example:
<form>
<input type="checkbox" name="sports" id="soccer">
<label for="soccer">Soccer</label>
<input type="checkbox" name="sports" id="cricket">
<label for="cricket">Cricket</label>
<input type="checkbox" name="sports" id="baseball">
<label for="baseball">Baseball</label>
</form>
Note: You can also create buttons using the <button> element. Buttons created
with the <button> element function just like buttons created with the input
element, but they offer richer rendering possibilities by allowing the embedding
of other HTML elements.
Example:
<form>
<label for="address">Address:</label>
<textarea rows="3" cols="30" name="address" id="address"></textarea>
</form>
required required Specifies that the user is required to select a value before
submitting the form
Example:
<form>
<label for="city">City:</label>
<select name="city" id="city">
<option value="sydney">Sydney</option>
<option value="melbourne">Melbourne</option>
<option value="cromwell">Cromwell</option>
</select>
</form>
Label Tag
The <label> tag defines the label for <input>, <select>, or <textarea> element.
Syntax:
<label> form content... </label>
Fieldset Tag
HTML <fieldset> tag is used to group the logically related fields/labels contained
within an HTML form.
The use of this tag is optional while creating an HTML form but using <filedset>,
it is easy to understand the purpose of grouped elements of form.
The <legend> tag is used with the <fieldset> element as a first child to define the
caption for the grouped related fields.
Syntax:
<fieldset>.....</fieldset>