Forms can be used to send data across the web and are often used as contact form to convert information input by a user into Leads. HTML forms are used to pass data to the server.
The common elements used in HTML form are form tag, input, textarea,, select, and label.

HTML Form Elements


HTML Form Tag

Form Tag defines the form and within this tag, there is action attribute which tells the form where its contents will be sent to when it is submitted.
An HTML form can contain input elements like text fields, checkbox, radio buttons, submit button and more. A form can also contain select list, textarea, fieldset, legend, and label elements.

Create HTML Form

form is build inside <form> tag. See the code below


<form action="" method="get" >
	/*Content*/
</form>

Form Attributes

Attribute Name Values Use
method get or post http get method submit form data but is visible in url.
post includes data in body. more secure as data is not visible to user in url
action path the backend file collecting form data
name any name name of form control

Fieldset

Form or form controls can also be placed inside fieldset tag. Fieldset tag is used to group form or multiple input controls. Fieldset group form controls in bordered area. We can also use legend tag inside fieldset.

The main functionality of Fieldset is to disable multiple form controls.

Fieldset with Legend

Enquiry Form


<fieldset>		
<legend> Enquiry Form</legend>		
<form action="" >
	<label>Name:<input type="text"></label>
    <input type="reset">
    <input type="submit">
</form>
</fieldset>

Fieldset with disabled

Fieldset also supports disabled attribute. By adding disables attribute, all form controls including submit button are disabled. Thus user cannot fill and submit form data.

Enquiry Form
Name:


<fieldset disabled>		
<legend> Enquiry Form</legend>		
<form action="" >
    <label>Name:<input type="text"></label>
    <input type="reset">
    <input type="submit">
</form>
</fieldset>

Input Element

The most important form element is the input element. The input element is used to get user information. An input element can be of type textbox, password, checkbox, radio buttons, submit button and more.


Attributes in Input element

Attribute Name values Use
type text, password, file, radio, checkbox, button, submit, and reset type defines type of input control.
size default value is 20 change size of input control
tabindex any numeric value used to define a sequence followed by user when he navigate using Tab key
value any possible value set a default value of input control
maxlength n digits set maximum characters length
disabled "" disabled input control, or fieldset tag
checked "" choose checkbox and radio button

Input Type Text

Input type text <input type="text"> is the common input element for name, surname, country, numbers and symbols. Default input type is text. Even if we skip type attribute in input element, default input type is text. But still it is recommended to define type atribute in input element.

HTML Input type text


First Name: <input type="text">  
Last Name: <input type="text"> 

HTML Input with label

Label is used to write the content just before text field. To Specify particular label, the value of for attribute inside label should match the id of input control..



<label>First Name:<input type="text"></label>

<label for="lname">Last Name:</label>
<input type="text" id="lname"> 


Input Type Text with value

value attribute can also be used inside input or textarea. Usually we ask user to fill values, but if value is fixed, use value attribute.



<form>
<label for="fname">First Name:</label> <input type="text" value="First Name" id="fname" >
<label for="lname">Last Name:</label><input type="text" value="Last Name" id="lname" >
</form>

Input Type Text with maxlength


maxlength attribute is used to restrict no of characters in a text field.

Input with maxlength



<form>
<label for="fname">First Name:</label><input type="text" id="fname" maxlength="10">
<label for="lname">Last Name:</label><input type="text" id="lname" maxlength="10">
</form>

Input type Password

The input type password is used to write passwords. The password value is written in encrypt form. i.e. a user cannot see, copy or cut password data from input type password.


<form>
<label for="pwd">Password:</label> 
<input type="password" id="pwd" >
</form>

Input type File

Input type file let user to choose file from his system. This can be used to uplaod a picture, upload resume, upload a video or audio etc.

Default value of input type file is "No file choosen". Once the file is uploaded, the file name replace this text followed by extension.


<form>
<label for="resume">Resume:</label> 
<input type="file" id="resume" >
</form>


Radio Buttons

Radio Buttons are used to choose a single element among a group.
To allow window to choose a single radio, use name attribute with same value on both radio inputs.

Create Radio Button group




    <input type="radio" name="gender" id="male"> 
    <label for="m">Male</label>
    
    <input type="radio" name="gender" id="female"> 
    <label for="f">Female</label>

Checkbox

Checkbox are used to select multiple selections unlike radio button. They can be checked and unchecked. We can use checkboxs for hobbies, interestes, terms & conditions, etc.

HTML Checkbox




<label> <input type="checkbox"> :Bike</label>
<label> <input type="checkbox"> :Car</label>

Checkbox with disabled

Checbox can also have disabled attribute. A disabled checbox can't be checked, means checked will remain checked, and unchecked will remain unchecked. See example

HTML Checkbox with disable


<label> <input type="checkbox" disabled> :I Agree</label>

Checkbox with checked

Default checbox state is unchecked. But we can change default state to checked by using checked attribute in input type checkbox. See example

HTML Checkbox with checked



<label> <input type="checkbox" checked> :I Agree</label>
<label> <input type="checkbox" disabled checked> :I Agree</label>

Select Dropdown

select or select dropdown is used to fetch single or multiple options in dropdown list. Select options are fixed, thus used can choose only given option or options. To select city, country, date, month, year etc, Select Dropdown is used.

Select dropdown example


<label for="city">Current City:</label>
<select id="city">
    <option selected disabled>--Select City--</option>
    <option>New Delhi</option>
    <option>Kolkata</option>
    <option>Mumbai</option>
    <option>Chennai</option>
</select>

Select with Optgroup

Optgroup element is used to group multiple options in select dropdown. The name of optgroup is set using label attribute in optgroup.


<label for="city">Current City:</label>
<select id="city">
    <option selected disabled>--Select City--</option>
    <optgroup label="Metros">
    	<option>New Delhi</option>
    	<option>Kolkata</option>
    	<option>Mumbai</option>
    	<option>Chennai</option>
    </optgroup>
	<optgroup label="Others">
        <option>Noida</option>
        <option>Gurgram</option>
        <option>Faridabad</option>
        <option>Gaziabad</option>					
    </optgroup>
</select>

Multiple Attribute

Multiple attribute is used in select dropdown to select more than one options by using Ctrl or Cmd key.

Press Ctrl or Cmd for multi selections.

<label for="city">Choose Multiple Cities:</label>
    
<select id="city" multiple>
    <option selected disabled>--Select City--</option>
    <optgroup label="Metros">
    	<option>New Delhi</option>
    	<option>Kolkata</option>
    	<option>Mumbai</option>
    	<option>Chennai</option>
    </optgroup>
    <optgroup label="Others">
    	<option>Noida</option>
    	<option>Gurgram</option>
    	<option>Faridabad</option>
    	<option>Gaziabad</option>					
    </optgroup>
</select>

Choose Date Of Birth using Select Dropdown




<select>
    <option selected disabled>--Choose Month--</option>
    <option>JAN</option>
    <option>FEB</option>
    <option>MAR</option>
    <option>APR</option>
    <option>MAY</option>
    <option>JUN</option>
    <option>JUL</option>
    <option>AUG</option>
    <option>SEP</option>
    <option>OCT</option>
    <option>NOV</option>
    <option>DEC</option>
</select>

<select>
    <option selected disabled>--Choose Date--</option>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    <option>10</option>
    <option>11</option>
    <option>12</option>
    <option>13</option>
    <option>14</option>
    <option>15</option>
    <option>16</option>
    <option>17</option>
    <option>18</option>
    <option>19</option>
    <option>20</option>
    <option>21</option>
    <option>22</option>
    <option>23</option>
    <option>24</option>
    <option>25</option>
    <option>26</option>
    <option>27</option>
    <option>28</option>
    <option>29</option>
    <option>30</option>
    <option>31</option>
</select>


Textarea

Textarea is used to write multiple line. Like post, query, address, comments, reviews etc. Textarea can have row and col attributes. Default rows are 2, and default columnns are 20.

Textarea Example








<textarea name="textarea" ></textarea>

<textarea name="textarea" rows="4"></textarea>

<textarea name="textarea" cols="30" rows="4"></textarea>

Submit Button

Submit Button or input type submit is used to send information from user to web server. Submit button can be used only once in a form tag.

Submit Button example


<form>
<input  type="submit"  value="submit">
</form>

Reset Button

Reset Button or input type reset is used to reload form data, without refreshing webpage. Reset is also used once in a form tag.

Create reset Button


<form>
<input type="text">
<input type="reset">
</form>

Button Tag

Button Tag or Button can alose create buttons, like input type button. Button Tag is pair element. We can use image, icon or child element inside button tag.

Type of buttons

  • Button,   <button>Button</button>.
  • Reset button,   <button type="reset">Button</button>.
  • Submit button,   <button type="submit">Button</button>.

Button Tag Example



<button>Button 1</button>
<button type="button">Button 2</button>
<button type="reset">Reset</button>
<button type="submit">Submit</button>

HTML Form Example

A complete HTML Form with all inputs, select dropdown, radio buttons, checkbox, textarea, submit and reset buttons.


    <form>
        <table>
            <tr>
                <td>
                    <label for="name">Name</label>
                </td>
                <td>
                    <input type="text" id="name" name="username">
                </td>
            </tr>
            <tr>
                <td>
                    <label for="email">Email</label>
                </td>
                <td>
                    <input type="text" id="email" name="usermail">
                    <button type="button">Check</button>
                </td>
            </tr>
            <tr>
                <td>
                    <label for="age">Age</label>
                </td>
                <td>
                    <input type="text" name="userage" id="age" size="2" maxlength="2">
                </td>
            </tr>
            <tr>
                <td>
                    <label>Country</label>
                </td>
                <td>
                    <input type="text" value="India" name="country" disabled>
                </td>
            </tr>
            <tr>
                <td>
                    <label for="pass">Password</label>
                </td>
                <td>
                    <input type="password" id="pass">
                </td>
            </tr>
            <tr>
                <td>
                    <label for="res">Resume</label>
                </td>
                <td>
                    <input type="file" id="res">
                </td>
            </tr>
            <tr>
                <td>
                    <label>Hobbies</label>
                </td>
                <td>
                    <label>
                        <input type="checkbox" checked> Cricket</label>
                    <label>
                        <input type="checkbox"> Football</label>
                </td>
            </tr>
            <tr>
                <td>
                    <label>Gender</label>
                </td>
                <td>
                    <label>
                        <input type="radio" value="f" name="gender"> Female</label>
                    <label>
                        <input value="m" type="radio" name="gender"> Male</label>
                </td>
            </tr>
            <tr>
                <td>
                    <label for="city">City</label>
                </td>
                <td>
                    <select id="city" name="city">
                        <option disabled selected>--Choose City--</option>
                        <optgroup label="Metros">
                            <option>New Delhi</option>
                            <option>Mumbai</option>
                            <option>Channai</option>
                            <option>Kolkata</option>
                        </optgroup>
                        <optgroup label="Others">
                            <option>Noida</option>
                            <option>Gurgram</option>
                            <option>Faridabad</option>
                            <option>Gaziabad</option>
                        </optgroup>
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    <label>Address</label>
                </td>
                <td>
                    <textarea rows="4" cols="40"></textarea>
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <input type="submit" value="Submit">
                    <input type="reset">
                </td>
            </tr>
        </table>
    </form>    

Inside form element, button will work as submit button, but outside form, button will work as button.


Html5 Form

These form controls are HTML4 Based. To learn HTML5 based Form controls and attributes, click here HTML5 Form