How to Create HTML Forms
What is HTML Form
HTML Forms are required to collect all kinds of user inputs, such as name, contact details, email address, phone numbers, and create a survey.
For Example: If anyone visits your site and wants to search for some product that you are selling so for this you need to create a search box. Also, a user wants to connect with you by filling the contact us form.
HTML Form Tags
HTML Form Syntax
<form action=“DataSaveLocationURL” method=“get|post”>
//input controls e.g. textfield, textarea, radiobutton, button
</form>
The <label> Element
The <label> tag represents a label for many form elements. It explains, for which the field is used. The for the attribute of the <label> tag should be equal to the id attribute of the <input> element to bind them together.
<form action=”” method=”get”>
<label for=“username”>Enter Username : </label>
</form>
Input Element
The <input type=”text” value=””> defines a single-line input field for text input. It is used to create form fields, to take input from the user. The type attribute can be of the type text field, password field, checkbox, radio button, submit button, reset button, file select box, etc.
<form>
<label for=“username”>Enter Username : </label>
<input type=“text” name=“username” id=“username”>
</form>
Password Field
Password fields are similar to text fields. The only difference is; characters in a password field are masked, i.e. they are shown as asterisks or dots.
Radio Buttons
Radio buttons are used to let the user select exactly one option from a pre-defined set of options. The name of a group of radio buttons should be equal.
<form>
<input type=“radio” id=“male” name=“gender” value=“male”>
<label for=“male”>Male</label><br>
<input type=“radio” id=“female” name=“gender” value=“female”>
<label for=“female”>Female</label><br>
<input type=“radio” id=“other” name=“gender” value=“other”>
<label for=“other”>Other</label>
</form>
Checkboxes
Checkboxes let a user select NONE or MORE options of choices. The name for a group of checkboxes should same.
<form>
<input type=“checkbox” name=“department” id=“Sports”>
<label for=“Sports”>Sports</label>
<input type=“checkbox” name=“department” id=“Other”>
<label for=“Other”>Other</label>
</form>
Textarea
Textarea is a multiple-line text input control that permits a user to enter more than one line of text
Select Boxes
A select box is a dropdown list of options that allows users to select one or more options from a pull-down list of options. Select box is created using the <select> element and <option> element.
<form>
<label for=“state”>State:</label>
<select name=“state” id=“state”>
<option value=“Odisha”>Odisha</option>
<option value=“Other”>Other</option>
</select>
</form>
Submit and Reset Buttons
The <input type=”submit”> defines a button for submitting the form data to a form-handler. and type=”reset” will reset your form fields.
The complete Form Code
Video Tutorial
editor's pick
news via inbox
Nulla turp dis cursus. Integer liberos euismod pretium faucibua