HTML Forms
Course Content
Describe, exemplify and implement form elements:
form element input:
text
number
textarea
radio
submit
form element select:
Describe, exemplify and implement form data validation:
length
presence
range
Why use HTML forms?
HTML forms are used to capture some input from a user and then process it in some way.Â
Such as the example below for the SQA calendar builder.
Different Form Elements
There are various form controls you may see:
Text BoxesÂ
Radio Buttons
Drop Down Menus (Combo Boxes)
Number Pickers
Submit Buttons
Text Box
Radio Buttons
Submit Button
Text Area
Number Picker
Drop Down Menus/Combo Boxes
Text Input Example
These are primarily used to allow a user to input smaller pieces of text.
The above text example demonstrates the structure of a text field. There are properties to set the name, width and height. There is also a length check as well as a presence check coded.
Number Input Example
These look similar to a text box except from the fact that it wont allow alphabetic data and there are up and down arrows (in some browsers) that will allow you to increment and decrement the values. You can also set minimum and maximum values.
Range Check Example
The code below will force the value to be between 1 and 3, the min and max attributes set these values.Â
<input type="number" name="tickets" min="1" max="3">
HTML5 will validate the input to see if meets the minimum and maximum values.
Text Area Example
A larger text box, for use with extended text input, can be implemented using the textarea form element. You can set the usual presence and length check but in addition you can change the height and width of the text box.
Radio Buttons Example
Radio inputs can be implemented using multiple input elements of type radio (only one option can be selected from a group). The checked property will set the default option.
Output from the above code is shown to the right. You will notice that the first option has already been selected.
Choose your age:<br>
<input type="radio" name="age" value="12 to 14" checked> 12-14
<br>
<input type="radio" name="age" value="15 or 16"> 15,16
<br>
<input type="radio" name="age" value="17 or over"> over 16
When submitted, the above form would return the named attribute “age” along with one of the listed values: 12 to 14, 15 or 16, 17 or over. Although not required at Higher, it is good practice to include both the name and value attributes.
If the <br> elements are omitted from the form, the radio buttons align horizontally.
Submit Button
When a form is submitted, the browser shows any validation errors (check browser versions, as this is browser dependent). To allow some visual acknowledgement that an action is performed when the submit button is clicked, a JavaScript onclick event can be applied to the form as shown below:
<form action="" onsubmit="alert ('Form Entered')" >
The output produced from the above code.
Drop down menus
The select element is used to create a list of possible inputs in the form of a drop-down menu. Input choices are placed inside option elements.
Setting the size of a drop down box
The size attribute can be used within the select element, to display a set number of options. If the number of options is larger than the size attribute, a scroll bar will automatically appear:
<select name="play" size="3">
You will now see 3 options.
Drop down + multiple attributes
To allow users to select more than one option, the multiple attribute is used with the select element:
<select name="play" size="4" multiple>
A select element allowing multiple selections
Pre-populating input
To aid user input, form elements can be given values that are displayed when the web page loads. These can be left unchanged, deleted or edited by the user, when they are completing the form.
Pre-populating text boxes
<input type="text" name="firstname" size="30" maxlength="15" required value = "Forename">
Pre-populating text areas
To pre-populate a textarea element, the text is included between the start and end elements as can be seem below:
<textarea name="message" rows="3" cols="50"> none </textarea>
Placeholder Values
To aid user input, form elements can be given placeholder suggestions that are displayed when the web page loads. These placeholders are replaced as soon as the user enters any new data.
The placeholder value will be replaced as soon as the user enters any data.
<input type="text" name="firstname" size="30" maxlength="15" required placeholder = "Enter forename here">
Pre-populating numeric values
<input type="number" name="tickets" min = "1" max="3" value = "2">
The value 2 will be entered initially.
Preselecting Option buttons
To pre-select an option button, you can select one value to be set as checked.
Choose your age:<br>
<input type="radio" name="age" value="12 to 14" checked> 12-14
<input type="radio" name="age" value="15 or 16"> 15,16
<input type="radio" name="age" value="17 or over"> over 16
The first option button will be selected (checked) by default.