HTML Form
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.
HTML forms are required if you want to collect some data from of the site visitor.
For example: If a user want to purchase some items on internet, he/she must fill the form such as shipping address and credit/debit card details so that item can be sent to the given address.
<form action="server url" method="get">
//input controls e.g. textfield, textarea, radiobutton, button
</form>
OR
<form action="server url" method="post">
//input controls e.g. textfield, textarea, radiobutton, button
</form>
List of HTML 5 form tags.
Tag | Description |
---|---|
<form> | It defines an HTML form to enter inputs by the used side. |
<input> | It defines an input control. |
<textarea> | It defines a multi-line input control. |
<label> | It defines a label for an input element. |
<fieldset> | It groups the related element in a form. |
<legend> | It defines a caption for a <fieldset> element. |
<select> | It defines a drop-down list. |
<optgroup> | It defines a group of related options in a drop-down list. |
<option> | It defines an option in a drop-down list. |
<button> | It defines a clickable button. |
List of HTML 5 form tags.
Tag | Description |
---|---|
<datalist> | It specifies a list of pre-defined options for input control. |
<keygen> | It defines a key-pair generator field for forms. |
<output> | It defines the result of a calculation. |
The type="text" attribute of input tag creates textfield control also known as single line textfield control. The name attribute is optional, but it is required for the server side component such as JSP, ASP, PHP etc.
<form>
First Name: <input type="text" name="firstname"/> <br/>
Last Name: <input type="text" name="lastname"/> <br/>
</form>
It is considered better to have label in form. As it makes the code parser/browser/user friendly.
If you click on the label tag, it will focus on the text control. To do so, you need to have for attribute in label tag that must be same as id attribute of input tag.
<form>
<label for="firstname">First Name: </label>
<input type="text" id="firstname" name="firstname"/> <br/>
<label for="lastname">Last Name: </label>
<input type="text" id="lastname" name="lastname"/> <br/>
</form>
The password is not visible to the user in password field control.
<form>
<label for="password">Password: </label>
<input type="password" id="password" name="password"/> <br/>
</form>
The email field in new in HTML 5. It validates the text for correct email address. You must use @ and . in this field.
<form>
<label for="email">Email: </label>
<input type="email" id="email" name="email"/> <br/>
</form>
The radio button is used to select one from multiple options. It is used in gender, quiz questions etc.
If you use one name for all the radio buttons, only one radio button can be selected at a time.
<form>
<label for="gender">Gender: </label>
<input type="radio" id="gender" name="gender" value="male"/>Male
<input type="radio" id="gender" name="gender" value="female"/>Female <br/>
</form>
The checkbox control is used to check multiple options from given checkboxes.
<form>
Courses:<br>
<input type="checkbox" id="java" name="java" value="java"/>
<label for="java">Java</label>
<input type="checkbox" id="sql" name="sql" value="sql"/>
<label for="sql">Sql</label>
<input type="checkbox" id="php" name="php" value="php"/>
<label for="php">Php</label>
</form>
Simple example of creating HTML form.
<form action="#">
<table>
<tr>
<td class="tdLabel"><label for="register_name" class="label">Enter name:</label></td>
<td><input type="text" name="name" value="" id="register_name" style="width:160px"/></td>
</tr>
<tr>
<td class="tdLabel"><label for="register_password" class="label">Enter password:</label></td>
<td><input type="password" name="password" id="register_password" style="width:160px"/></td>
</tr>
<tr>
<td class="tdLabel"><label for="register_email" class="label">Enter Email:</label></td>
<td
><input type="email" name="email" value="" id="register_email" style="width:160px"/></td>
</tr>
<tr>
<td class="tdLabel"><label for="register_gender" class="label">Enter Gender:</label></td>
<td>
<input type="radio" name="gender" id="register_gendermale" value="male"/>
<label for="register_gendermale">male</label>
<input type="radio" name="gender" id="register_genderfemale" value="female"/>
<label for="register_genderfemale">female</label>
</td>
</tr>
<tr>
<td class="tdLabel"><label for="register_country" class="label">Select Country:</label></td>
<td><select name="country" id="register_country" style="width:160px">
<option value="india">india</option>
<option value="pakistan">pakistan</option>
<option value="africa">africa</option>
<option value="china">china</option>
<option value="other">other</option>
</select>
</td>
</tr>
<tr>
<td colspan="2"><div align="right"><input type="submit" id="register_0" value="register"/>
</div></td>
</tr>
</table>
</form>
Element | Chrome | IE | Firefox | Opera | Safari |
<h1> to <h6> | Yes | Yes | Yes | Yes | Yes |
© 2025 Easy To Learning. All Rights Reserved | Design by Easy To Learning