HTML Form is used to create user input.
There are several types of form controls that you can use to collect information from visitors to your website.
Text input (text box): It used for a single line of text such as usernames.
<input> The input element is used to create several different form controls. The value of the type attribute determines what kind of input they will be creating.
type="text" When the type attribute has a value of text, it creates a singleline text input.
name When users enter information into a form, the server needs to know which form control each piece of data was entered into.(Example, in a register form, the server needs to know what has been entered as the name and other information.
Password input: Like a single line text box but it masks the characters enter in the text field.
type="password" When the type attribute has a value of password it creates a text box that acts just like a single-line text input, except the characters are blocked out. They are hidden in this way so that if someone is looking over the user's shoulder, they cannot see sensitive data such as passwords.
name: The name attribute indicates the name of the password input, which is sent to the server with the password the user enters.
Although the password is hidden on the screen, this does not mean that the data in a password control is sent securely to the server. You should never use these for sending sensitive data such as credit card numbers.
Text area (multi-line): For longer areas of text, such as messages and feedback.
The <textarea> element is used to create a multi-line text input. Unlike other input elements this is not an empty element. It should therefore have an opening and a closing tag.
Any text that appears between the opening <textarea> and closing textarea> tags will appear in the text box when the page loads.
Radio buttons: For use when a user must select one of a number of options.
type="radio" Radio buttons allow users to pick just one of a number of options.
name: The name attribute is sent to the server with the value of the option the user selects. When a question provides users with options for answers in the form of radio buttons, the value of the name attribute should be the same for all of the radio buttons used to answer there question.
value: The value attribute indicates the value that is sent to the server for the selected option. The value of each of the buttons in a group should be different (so that the server knows which option the user has selected).
Once a radio button
has been selected it cannot be
deselected. The user can only
select a different option. If you
are only allowing the user one option and want them to be
able to deselect it (for example
if they are indicating they agree
to terms and conditions), you
should use a checkbox instead.
Checkboxes When a user can select and unselected one or more options.
type="checkbox" Checkboxes allow users to select or unselect one or more options in the given lists options.
name: The name attribute is sent to the server with the value of the option(s) the user selects. When a question provides users with options for answers in the form of checkboxes, the value of the name attribute should be the same for all of the buttons that answer that question.
value: The value attribute indicates the value sent to the server if this checkbox is checked.
Drop-down boxes When a user must pick one of a number of options from a list.
A drop down list box also known as a select box allows users to select one option from a drop down list.
The <select > element is used to create a drop down list box. It contains two or more <option> elements.
The <option> element is used to specify the options that the user can select from. The words between the opening <option> and closing </option> tags will be shown to the user in the drop down box.
The selected attribute can be used to indicate the option that should be selected when the page loads. The value of this attribute should be selected.
If this attribute is not used, the first option will be shown when the page loads. If the user does not select an option, then the first item will be sent to the server as the value for this control.
The function of the drop down list box is similar to that of the radio buttons in that only one option can be selected. There are two key factors in choosing which to use:
1. If users need to see all options at a glance, radio buttons are better suited.
2. If there is a very long list of options like list of family member drop down list boxes work better.
Submit buttons To submit data from your form to another web page
type="submit" The submit button is used to send a form to the server.
The value attribute is used to control the text that appears on a button. It is a good idea to specify the words you want to appear on a button because the default value of buttons on some
browsers is ‘Submit query’ and this might not be appropriate for all kinds of form.
Image buttons Similar to submit buttons but they allow you to use an image
File upload Allows users to upload files (e.g. images) to a website.
<input> If you want to allow users to upload a file like image, video, mp3, or a PDF, you will need to use a file input box.
type="file" This type of input creates a box that looks like a text input followed by a browse button. When the user clicks on the browse button, a window opens up that allows them to select a file from their computer to be uploaded to the website.
When you are upload files, the method attribute on the element must have a value of post. You can not send files using the HTTP get method.
Button & hidden Controls
The <button> element was introduced to allow users more control over how their buttons appear, and to allow other elements to appear inside the button.
This means that you can combine text and images between the opening <button> tag and closing </button> tag.
This example also shows a
hidden form control. These form
controls are not shown on the
page although you can see them
if you use the View Source option
in the browser.
Labelling Form Controls
When introducing form controls, the code was kept simple by indicating the purpose of each one in text next to it. However, each form control should have its own <label> element as this makes the form accessible to vision-impaired users.
The <label> element can be used in two ways. It can:
1. Wrap around both the text description and the form input (as shown on the first line of the example to your right).
2. Be kept separate from the form control and use the for attribute to indicate which form control it is a label for (as shown with the radio buttons).
Grouping Form Elements
You can group related form controls together inside the <fieldset>element. This is particularly helpful for longer forms.
Most browsers will show the<fieldset> with a line around the edge to show how they are related. The appearance of these lines can be adjusted using CSS.
<legend>: The <legend> element can come directly after the opening <fieldset> tag and contains a caption which helps identify the purpose of that group of form controls.
HTML form may have several form controls, each gathering different information. The server needs to new which piece of inputted data corresponds with which from element.
controls live inside element.This element should always carry the action attribute and will usually have a method attribute too.
action="index.php" Every element requires an action attribute. Its value is the URL for the page on the server that will receive the information in the form when it is submitted
method Forms can be sent using one of two methods: GET or POST.
With the GET method, the values from the form are added to the end of the URL specified in the action attribute. The best is for the get method:
- short forms (such as search boxes)
- when you are just retrieving data from the web server it not sending information that should be added or deleted from a database
With the POST method, the values are sent are known as HTTP headers. As a rule of thumb you should use the post method if your form:
- allows user to upload a file.
- is very long.
- contains sensitive data like passwords.
- adds or deletes information from a database.
- If the method attribute is not used, the form data will be sent using the get method.
HTML5: Form Validation
You have probably seen forms on the web that give users messages if the form control has not been filled in correctly; this is known as form validation.
Validation helps ensure the user enters information in a form that the server will be able to understand when the form is submitted. Validating the contents of the form before it is sent to the server the helps:
- Reduce the amount of work the server has to do.
- Enables users to see if there are problems with the form faster than if validation were performed on the server.
Leave a comment
No Cmomments yet