HTML Form: An HTML structure is used to gather user input. The user input is frequently sent to a server for processing.
In this tutorial, you will learn-
In this article, you will learn-
What is HTML Form
HTML Forms are needed to collect various types of user inputs, for example, contact details like name, email address, telephone numbers, or details like charge card data, and so on
Form contain special elements called controls like inputbox, checkboxes, radio-catches, submit catches, and so forth Users for the most part total a form by adjusting its controls for example entering text, choosing items, and so forth and submitting this form to a web server for additional processing.
HTML Forms
If you click the “Submit” button, the form-data will be sent to a page called “/action_page.php”.
<!DOCTYPE html> <html> <body> <h2>HTML Forms</h2> <form action="/action_page.php"> <label for="fname">First name:</label><br> <input type="text" id="fname" name="fname" value="Salman"><br> <label for="lname">Last name:</label><br> <input type="text" id="lname" name="lname" value="Khan"><br><br> <input type="submit" value="Submit"> </form> <p>If you click the "Submit" button, the form-data will be sent to a page called "/action_page.php".</p> </body> </html>
The <form> Element
The HTML <form> element is used to create an HTML form for user input:
<form> . form elements . </form>
The <form> element is a container for various sorts of input elements, for example, text fields, checkboxes, radio catches, submit catches, and so on
The <input> Element
The HTML <input> element is the most used form element.
An <input> element can be shown from numerous points of view, depending on the type attribute.
Here are some examples:
Type | Description |
---|---|
<input type=”text”> | Displays a single-line text input field |
<input type=”radio”> | Displays a radio button (for selecting one of many choices) |
<input type=”checkbox”> | Displays a checkbox (for selecting zero or more of many choices) |
<input type=”submit”> | Displays a submit button (for submitting the form) |
<input type=”button”> | Displays a clickable button |
Text Fields
The <input type=”text”>defines a single-line input field for text input.
Example
A form with input fields for text:
<form> <label for="fname">First name:</label><br> <input type="text" id="fname" name="fname"><br> <label for="lname">Last name:</label><br> <input type="text" id="lname" name="lname"> </form>
<!DOCTYPE html> <html> <body> <h2>Text input fields</h2> <form> <label for="fname">First name:</label><br> <input type="text" id="fname" name="fname" value="Salman"><br> <label for="lname">Last name:</label><br> <input type="text" id="lname" name="lname" value="Khan"> </form> <p>Note that the form itself is not visible.</p> <p>Also note that the default width of text input fields is 20 characters.</p> </body> </html>
Text input fields
Note that the form itself is not visible.
Also note that the default width of text input fields is 20 characters.
Note: The form itself is not visible. Likewise note that the default width of an input field is 20 characters.
The <label> Element
Notice the use of the <label> element in the example above.
The <label> tag defines a label for some form elements.
The <label> element valuable for screen-reader users, on the grounds that the screen-reader will read out loud the label when the user focus on the input element.
The <label> element likewise help users who experience issues tapping on very small regions (like radio catches or checkboxes) – on the grounds that when the user taps the content inside the element, it flips the radio catch/checkbox.
The for quality of the <label> tag ought to be equivalent to the id attribute of the <input> element to bind them together
Radio Buttons
The <input type=”radio”> defines a radio catch.
Radio catches let a user select ONE of a limited number of decisions.
Example
A form with radio buttons:
<p>Choose your favorite Web language:</p> <form> <input type="radio" id="html" name="fav_language" value="HTML"> <label for="html">HTML</label><br> <input type="radio" id="css" name="fav_language" value="CSS"> <label for="css">CSS</label><br> <input type="radio" id="javascript" name="fav_language" value="JavaScript"> <label for="javascript">JavaScript</label> </form>
This is how the HTML code above will be displayed in a browser:
Choose your favorite Web language:
Radio Buttons
Choose your favorite Web language:
Checkboxes
The <input type=”checkbox”> defines a checkbox.
Checkboxes let a user select ZERO or MORE alternatives of a limited number of decisions.
Example
A form with checkboxes:
<form> <input type="checkbox" id="vehicle1" name="vehicle1" value="Bike"> <label for="vehicle1"> I have a bike</label><br> <input type="checkbox" id="vehicle2" name="vehicle2" value="Car"> <label for="vehicle2"> I have a car</label><br> <input type="checkbox" id="vehicle3" name="vehicle3" value="Boat"> <label for="vehicle3"> I have a boat</label> </form>
This is how the HTML code above will be displayed in a browser:
Checkboxes
The input type=”checkbox” defines a checkbox:
The Submit Button
The <input type=”submit”> defines a catch for submitting the form data to a form-handler.
The form-handler is typically a file on the server with a content for processing input data.
The form-handler is determined in the form’s actions attribute.
Example
A form with a submit button:
<form action="/action_page.php"> <label for="fname">First name:</label><br> <input type="text" id="fname" name="fname" value="Salman"><br> <label for="lname">Last name:</label><br> <input type="text" id="lname" name="lname" value="Khan"><br><br> <input type="submit" value="Submit"> </form>
This is how the HTML code above will be displayed in a browser:
HTML Forms
If you click the “Submit” button, the form-data will be sent to a page called “/action_page.php”.
The Name Attribute for <input>
Notice that each input field should have a name attribute to be submitted.
On the off chance that the name attribute is excluded, the value of the input field won’t be sent by any means.
Example
This example will not submit the value of the “First name” input field:
<form action="/action_page.php"> <label for="fname">First name:</label><br> <input type="text" id="fname" value="Salman"><br><br> <input type="submit" value="Submit"> </form>
The name Attribute
If you click the “Submit” button, the form-data will be sent to a page called “/action_page.php”.
Notice that the value of the “First name” field will not be submitted, because the input element does not have a name attribute.
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your projects with us.