HTML Forms | How to Create Forms in HTML
To create form In HTML, We make use of the HTML <form> Elements. This Element Defines a form That is Used to Collect Users Input:
For Example:
If you click the "Submit" button, the form-data will be sent to a page called "action_page.php".
If you click the "Submit" button, the form-data will be sent to a page called "action_page.php".
There are different types of HTML Form Elements, Like Text fields, radio buttons, submit buttons, Checkboxs, Fieldsets and More..
The <input> Element
The <input> element is the most important form element.The <input> element can be displayed in several ways, depending on the type attribute.
For Example:
<input type="text"> : Defines a one-line text input field.
<input type="radio"> : Defines a radio button (for selecting one of many choices)
<input type="submit"> : Defines a submit button (for submitting the form)
The Text Input
<input type="text"> defines a one-line input field for text input:Note: The form itself is not visible. Also note that the default width of a text field is 20 characters.
Radio Button Input
<input type="radio"> defines a radio button.Radio buttons let a user select ONE of a limited number of choices:
The Submit Button
<input type="submit"> defines a button for submitting the form data to a form-handler.The form-handler is typically a server page with a script for processing input data.
The form-handler is specified in the form's action attribute:
The Action Attribute
The action attribute written as:<form action="action_page.php">
defines the action to be performed when the form is submitted.
Normally, the form data is sent to a web page on the server when the user clicks on the submit button.
In the example above, the form data is sent to a page on the server called "action_page.php". This page contains a server-side script that handles the form data.
If the action attribute is omitted, the action is set to the current page.
The Method Attribute
The method attribute specifies the HTTP method (GET or POST) to be used when submitting the form data:Written:
<form action="action_page.php" method="get"> or <form action="action_page.php" method="post">
When To Use The Get and Post Methods
When to Use GET?
The default method when submitting form data is GET.However, when GET is used, the submitted form data will be visible in the page address field:
Example:
action_page.php?firstname=Mickey&lastname=Mouse
Note: GET must NOT be used when sending sensitive information! GET is best suited for short, non-sensitive, amounts of data, because it has size limitations too.
When to Use POST?
Always use POST if the form data contains sensitive or personal information. The POST method does not display the submitted form data in the page address field.POST has no size limitations, and can be used to send large amounts of data.
The Name Attribute
Each input field must have a name attribute to be submitted.If the name attribute is omitted, the data of that input field will not be sent at all.
The example below will only submit the "Last name" input field:
Grouping Form Data with <fieldset>
The <fieldset> element is used to group related data in a form.The <legend> element defines a caption for the <fieldset> element.
Example
<form action="action_page.php">
<fieldset>
<legend>Personal
information:</legend>
First name:<br>
<input type="text" name="firstname"
value="Godson"><br>
Last name:<br>
<input type="text" name="lastname" value="Smart"><br><br>
<input type="submit" value="Submit">
</fieldset>
</form>
The above HTML Code will be displayed in your Browser like this:
Here is the list of <form> attributes:
Attribute | Description |
---|---|
accept-charset | Specifies the charset used in the submitted form (default: the page charset). |
action | Specifies an address (url) where to submit the form (default: the submitting page). |
autocomplete | Specifies if the browser should autocomplete the form (default: on). |
enctype | Specifies the encoding of the submitted data (default: is url-encoded). |
method | Specifies the HTTP method used when submitting the form (default: GET). |
name | Specifies a name used to identify the form (for DOM usage: document.forms.name). |
novalidate | Specifies that the browser should not validate the form. |
target | Specifies the target of the address in the action attribute (default: _self). |
You will learn more about the form attributes in the next chapters.
Click here if you are New To Web Design in HTML: How To Make Website/Web Pages Using Text Editors e.g NotePad++