Resources:
The HTML <form> element is the parent element that defines a form that is used to collect user input data using form elements.
<form>
form elements goes here... (will be discussed later)
</form>
There are several attributes that are associated with the form element.
The two major form attributes are the action and the method. The target attribute can also be used.
<form action="my_form_script.php" method="get" target="_blank">
form elements goes here...
</form>
The HTML form elements are the children elements of the parent <form> tag. There are three major form elements (input, select, and text).
The <input> element is the most important form element that can be displayed in several ways depending on the type attribute.
NOTES:
Below is a list of examples of the most common input HTML form types:
The <input type="text"> defines a one-line text input field. it is used with the <label> element along with the "for" and "id" attributes.
CODE VIEW:
<form action="my_form_script.php" method="get" target="_blank">
<label for ="firstname">First Name:</label>
<input type="text" name="firstname" id="firstname"><br>
<label for ="firstname">Last Name:</label>
<input type="text" name="firstname" id="firstname"><br>
</form>
NOTE; The "for" attribute is is used to associate the <label> form with the <input> form "id" attribute. Hence, they need to have the SAME name to create the association between the two.
DESIGN VIEW:
NOTE: The default width is 20 characters. However, this can be changed with an attribute.
The <input type="radio"> - defines a radio button for selecting only one choice of many with the type, name and value attributes. This concept is know as mutually exclusive.
CODE VIEW:
<form action="my_form_script.php" method="get" target="_blank">
<label for ="firstname">First Name:</label>
<input type="text" name="firstname" id="firstname"><br>
<label for ="firstname">Last Name:</label>
<input type="text" name="firstname" id="firstname"><br><br>
<p>Gender:</p>
<input id="male" type="radio" name="gender" value="male"><label for="male">Male</label<br>
<input id = "female" type="radio" name="gender" value="female"><label for="female">Female</label>
</form>
NOTE: In order for a group of radio buttons to work properly, ALL of the name attributes has to have the SAME value. Hence, the radio button must have TWO ASSOCIATIONS:
DESIGN VIEW:
NOTE: You can only select one option at a time.
The <input type="checkbox"> defines a checkbox that let a user select ONE or MORE options.
CODE VIEW:
<form action="my_form_script.php" method="get" target="_blank">
<label for ="firstname">First Name:</label>
<input type="text" name="firstname" id="firstname"><br>
<label for ="firstname">Last Name:</label>
<input type="text" name="firstname" id="firstname"><br><br>
<p>Gender:</p>
<input id="male" type="radio" name="gender" value="male"><label for="male"> Male</label<br>
<input id = "female" type="radio" name="gender" value="female"><label for="female"> Female</label><br/><br/>
<p>How do you get around town?</p>
<input type="checkbox" name="walk" value="walk"><label for="walk">I walk</label><br/>
<input type="checkbox" name="bike" value="bike"><label for="bike">I bike</label><br/>
<input type="checkbox" name="bus" value="bus"><label for="bus">I take a bus</label><br/>
<input type="checkbox" name="car" value="car"><label for="car">I drive my car</label><br/>
<input type="checkbox" name="train" value="train"><label for="train">I take a train</label>
</form>
DESIGN VIEW:
The <input type="submit"> defines a button for submitting the form data to a form-handler that is typically a server script page to process the input data based on the form's action attribute.
NOTE: Unlike most of the value attributes that is used to send that value to a server, this value attribute is used to define the button label for both the submit button and the reset reset button that will be discussed next.
CODE VIEW:
<form action="my_form_script.php" method="get" target="_blank">
<label for ="firstname">First Name:</label>
<input type="text" name="firstname" id="firstname"><br>
<label for ="firstname">Last Name:</label>
<input type="text" name="firstname" id="firstname"><br><br>
<p>Gender:</p>
<input id="male" type="radio" name="gender" value="male"><label for="male"> Male</label<br>
<input id = "female" type="radio" name="gender" value="female"><label for="female"> Female</label><br/><br/>
<p>How do you get around town?</p>
<input type="checkbox" name="walk" value="walk"><label for="walk">I walk</label><br/>
<input type="checkbox" name="bike" value="bike"><label for="bike">I bike</label><br/>
<input type="checkbox" name="bus" value="bus"><label for="bus">I take a bus</label><br/>
<input type="checkbox" name="car" value="car"><label for="car">I drive my car</label><br/>
<input type="checkbox" name="train" value="train"><label for="train">I take a train</label><br/><br/>
<input type="submit" value="Register">
</form>
DESIGN VIEW:
The <input type="reset"> defines a reset button that will reset all form values to their default values:
CODE VIEW:
<form action="my_form_script.php" method="get" target="_blank">
<label for ="firstname">First Name:</label>
<input type="text" name="firstname" id="firstname"><br>
<label for ="firstname">Last Name:</label>
<input type="text" name="firstname" id="firstname"><br><br>
<p>Gender:</p>
<input id="male" type="radio" name="gender" value="male"><label for="male"> Male</label<br>
<input id = "female" type="radio" name="gender" value="female"><label for="female"> Female</label><br/><br/>
<p>How do you get around town?</p>
<input type="checkbox" name="walk" value="walk"><label for="walk">I walk</label><br/>
<input type="checkbox" name="bike" value="bike"><label for="bike">I bike</label><br/>
<input type="checkbox" name="bus" value="bus"><label for="bus">I take a bus</label><br/>
<input type="checkbox" name="car" value="car"><label for="car">I drive my car</label><br/>
<input type="checkbox" name="train" value="train"><label for="train">I take a train</label><br/><br/>
<input type="submit" value="Register">
<input type="reset" value="Clear Form">
</form>
DESIGN VIEW:
While you can create a button with the <button> element, a form button can be created with the <input type="button">.
CODE VIEW:
<form action="my_form_script.php" method="get" target="_blank">
<label for ="firstname">First Name:</label>
<input type="text" name="firstname" id="firstname"><br>
<label for ="firstname">Last Name:</label>
<input type="text" name="firstname" id="firstname"><br><br>
<p>Gender:</p>
<input id="male" type="radio" name="gender" value="male"><label for="male"> Male</label<br>
<input id = "female" type="radio" name="gender" value="female"><label for="female"> Female</label><br/><br/>
<p>How do you get around town?</p>
<input type="checkbox" name="walk" value="walk"><label for="walk">I walk</label><br/>
<input type="checkbox" name="bike" value="bike"><label for="bike">I bike</label><br/>
<input type="checkbox" name="bus" value="bus"><label for="bus">I take a bus</label><br/>
<input type="checkbox" name="car" value="car"><label for="car">I drive my car</label><br/>
<input type="checkbox" name="train" value="train"><label for="train">I take a train</label><br/><br/>
<input type="submit" value="Register">
<input type="reset" value="Clear Form"><br><br>
<input type="button" onclick="alert('More Info goes here...!')" value="More Info">
</form>
DESIGN VIEW:
NOTE: If the More Info button is clicked, a dialog box will be opened.
You can group related form elements together with the <fieldset> element which is used with the <legend> element that defines a caption for the fieldset elements.
CODE VIEW:
<form action="my_form_script.php" method="get" target="_blank">
<fieldset>
<legend>Personal information:</legend>
<label for ="firstname">First Name:</label>
<input type="text" name="firstname" id="firstname"><br>
<label for ="firstname">Last Name:</label>
<input type="text" name="firstname" id="firstname"><br><br>
<p>Gender:</p>
<input id="male" type="radio" name="gender" value="male"><label for="male"> Male</label<br>
<input id = "female" type="radio" name="gender" value="female"><label for="female"> Female</label><br/><br/>
</fieldset>
<p>How do you get around town?</p>
<input type="checkbox" name="walk" value="walk"><label for="walk">I walk</label><br/>
<input type="checkbox" name="bike" value="bike"><label for="bike">I bike</label><br/>
<input type="checkbox" name="bus" value="bus"><label for="bus">I take a bus</label><br/>
<input type="checkbox" name="car" value="car"><label for="car">I drive my car</label><br/>
<input type="checkbox" name="train" value="train"><label for="train">I take a train</label><br/><br/>
<input type="submit" value="Register">
<input type="reset" value="Clear Form"><br><br>
<input type="button" onclick="alert('More Info goes here...!')" value="More Info">
</form>
DESIGN VIEW:
<input type="password"> defines a password field:
SPECIAL INSTRUCTI0N: Add the following code ABOVE the previous opening <form> tag.
CODE VIEW:
<form action="my_form_script.php" method="get" target="_blank">
<fieldset>
<legend>Login:</legend>
<label name="username" for="username">UserName:</label>
<input type="text" name="username" id="username"><br>
<label name="password" for="password">Password:</label>
<input type="password" name="password" id="password"><br>
<input type="submit" value="Login">
</fieldset>
</form><br/><br/>
<form action="my_form_script.php" method="get" target="_blank">
<fieldset>
<legend>Personal information:</legend>
<label for ="firstname">First Name:</label>
<input type="text" name="firstname" id="firstname"><br>
<label for ="firstname">Last Name:</label>
<input type="text" name="firstname" id="firstname"><br><br>
<p>Gender:</p>
<input id="male" type="radio" name="gender" value="male"><label for="male"> Male</label<br>
<input id = "female" type="radio" name="gender" value="female"><label for="female"> Female</label><br/><br/>
</fieldset>
<p>How do you get around town?</p>
<input type="checkbox" name="walk" value="walk"><label for="walk"> I walk</label><br/>
<input type="checkbox" name="bike" value="bike"><label for="bike"> I bike</label><br/>
<input type="checkbox" name="bus" value="bus"><label for="bus"> I take a bus</label><br/>
<input type="checkbox" name="car" value="car"><label for="car"> I drive my car</label><br/>
<input type="checkbox" name="train" value="train"><label for="train"> I take a train</label><br/><br/>
<input type="submit" value="Register">
<input type="reset" value="Clear Form"><br><br>
<input type="button" onclick="alert('More Info goes here...!')" value="More Info">
</form>
DESIGN VIEW:
NOTE: When typing, the characters in a password field will be masked with asterisks or circles.
SPECIAL INSTRUCTI0N: Add the following style rules within a <style> element in the <head> element. Then, add the class to both forms parent tag (e.g., <form class="form_custom_width" action="my_form_script.php" method="get" target="_blank">
CODE VIEW:
DESIGN VIEW:
SPECIAL INSTRUCTI0N: Add a <button> element between form elements with a onClick attribute set to a function and two line break elements.
CODE VIEW:
</form><br/><br/>
<button onClick="showHideRegistration()">Register</button><br/><br/>
<form action="my_form_script.php" method="get" target="_blank">
CODE VIEW:
SPECIAL INSTRUCTI0N: Add id="registrationForm" to the second <form> element:
CODE VIEW:
SPECIAL INSTRUCTI0N: ABOVE the closing </head> tag, add the following code within <script> element in the <head> tag:
DESIGN VIEW:
HTML5 added several new input types. Types that are not supported by older web browsers, will behave as <input type="text">.
To see a complete list of all of the new HTML 5 Input Form Types, go to w3schools.
The <select> element defines a drop-down list: It is used with the <option> elements which defines an option that can be selected.
Code View:
DESIGN VIEW:
States you have visited:
By default, the first item in the drop-down list is selected. To define a pre-selected option, add the selected attribute to the option:
CODE VIEW:
DESIGN VIEW:
States you have visited:
NOTE: Notice Texas is the selected option instead of Louisiana.
To change the combo box to a List box add a size attribute to the select element to specify the number of visible values:
CODE VIEW:
DESIGN VIEW:
States you have visited:
NOTE: Notice the the combo box has been converted to a three rows List box.
To allow the user to select multiple options add the multiple attribute to the option element. It is best practice to write instruction to the user on how to select multiple options.
CODE VIEW:
DESIGN VIEW:
States you have visited:
Press the CTRL/CMD button to select multiple options.
NOTE: Press the CTRL/CMD key now will allow a user to select multiple options.
Unlike the <input type="type /> is use to define a SINGLE line text field, the <textarea> element defines a MULTI-LINE input field along with the row and column attributes to define the height and width of the text area.
CODE VIEW:
DESIGN VIEW:
NOTE: You can pull on the right-box corner to resize the text area box.
HTML5 added the following form elements. However, newer elements may not be supported by older browsers
The <datalist> element specifies a list of pre-defined options for an <input> element when data is entered. The list attribute of the <input> element, must refer to the id attribute of the <datalist> element. The datalist tag is not supported in Safari or IE9 or earlier,
NOTE: The "list" attribute is akin to the "for" attribute of the <input> element when using a <text> element and the list attribute is used to associate it with the "id" attribute of the the <datalist> element.
CODE VIEW:
<form action="my_form_script.php" method="get" target="_blank">
<p>What is your favorite color? (As you type value will be shown)</p>
<input list="colors">
<datalist id="colors">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Purple">Purple</option>
<option value="Yellow">Yellow</option>
</datalist>
</form>
DESIGN VIEW:
The <output> element represents the result of a calculation. It is not supported in Edge 12 or Internet Explorer or earlier.
CODE VIEW:
<form oninput="result.value=parseInt(a.value)+parseInt(b.value)">
<input type="range" name="b" value="50" /> +
<input type="number" name="a" value="10" /> =
<output name="result">60</output>
</form>
DESIGN VIEW:
There are a few HTML Attributes that has not been discussed:
The value attribute specifies the initial value for an input field:
CODE VIEW:
<form action="my_form_script.php" method="get" target="_blank">
<label for ="firstname">First Name:</label>
<input type="text" name="firstname" id="firstname" value="Cornelius"><br>
<label for ="firstname">Last Name:</label>
<input type="text" name="firstname" id="firstname"><br>
</form>
DESIGN VIEW:
The readonly attribute specifies that the input field is read only and cannot be changed:
CODE VIEW:
<form action="my_form_script.php" method="get" target="_blank">
<label for ="firstname">First Name:</label>
<input type="text" name="firstname" id="firstname" value="Cornelius" readonly ><br>
<label for ="firstname">Last Name:</label>
<input type="text" name="firstname" id="firstname"><br>
</form>
DESIGN VIEW:
The disabled attribute specifies that the input field is disabled and cannot be use or click on. Also, its value will not be submitted when the form is processed.
NOTE: Change readonly to disabled.
CODE VIEW:
<form action="my_form_script.php" method="get" target="_blank">
<label for ="firstname">First Name:</label>
<input type="text" name="firstname" id="firstname" value="Cornelius" disabled ><br>
<label for ="firstname">Last Name:</label>
<input type="text" name="firstname" id="firstname"><br>
</form>
DESIGN VIEW:
The size attribute specifies the size (in characters) for the input field:
Remove disabled attribute and add size="40"
CODE VIEW:
<form action="my_form_script.php" method="get" target="_blank">
<label for ="firstname">First Name:</label>
<input type="text" name="firstname" id="firstname" value="Cornelius" size="40"><br>
<label for ="firstname">Last Name:</label>
<input type="text" name="firstname" id="firstname"><br>
</form>
DESIGN VIEW:
The maxlength attribute specifies the maximum allowed length for the input field and will not accept more characters.
NOTE: Change size="40" to maxlength="10"
CODE VIEW:
<form action="my_form_script.php" method="get" target="_blank">
<label for ="firstname">First Name:</label>
<input type="text" name="firstname" id="firstname" value="Cornelius" maxlength="10"><br>
<label for ="firstname">Last Name:</label>
<input type="text" name="firstname" id="firstname"><br>
</form>
DESIGN VIEW:
HTML5 added the following attributes for the <input> element:
HTML5 added the following attributes for the <form> element::
For more examples of HTML 5 attributes, go to w3schools.