WS Lec4 HTML5 Part02 Forms 2
WS Lec4 HTML5 Part02 Forms 2
HTML5-Part02-Forms
Lecture 4
Note: All examples can be found at the bottom of page xxvi in the textbook: Internet and World Wide Web
How to Program - 5th Edition, Deitel & Deitel
Original slides are prepared by:
Dr. Abdullah Baqasah
Contents
1. Introduction
2. form Element
3. method & action Attributes of the
form Element
4. hidden Inputs
5. input Element
6. text input Element
7. Button input Elements
8. Additional Form Elements
Web Systems [502315-3] Dr. Afnan Bukhari 2
1. Introduction
Introduction
● When browsing websites, users often need to provide information such as search
queries, e-mail addresses and zip codes.
● HTML5 provides a mechanism, called a form, for collecting data from a user.
● Data that users enter on a web page is normally sent to a web server that
provides access to a site’s resources.
● When a browser requests a publicly available web page or file that’s located on a
server, the server processes the request and returns the requested resource.
● HTML5 documents are requested and transferred via the Hypertext Transfer
Protocol (HTTP).
● Figure 2.14 is a simple form that sends data to the web server for processing. The
web server typically returns a web page back to the web browser—this page
often indicates whether or not the form’s data was processed correctly.
● The JavaScript language can be used to make pages that “do something”
○ You can use JavaScript to write complete programs, but...
○ Usually, you just use snippets of JavaScript here and there
throughout your Web page
○ JavaScript code snippets can be attached to various form elements
■ For example, you might want to check that a zipcode field contains a 5-
digit integer before you send that information to the server
● Microsoft calls its version of JavaScript “active scripting”
● Forms can be used without JavaScript, and JavaScript can be used
without forms, but they work well together
● JavaScript for forms is covered in a separate lecture
● The <form attribute> . ... form elements. .. </form> tag encloses form elements (and probably
other elements as well)
● The attribute to form tell what to do with the user input
○ action="url" (required)
■ Specifies where to send the data when the Submit button is clicked
○ method="get" (default)
■ Form data is sent as a URL with ?form_data info appended to the end
■ Can be used only if data is all ASCII and not more than 100 characters
○ method="post"
■ Form data is sent in the body of the URL request
■ Cannot be bookmarked by most browsers
○ target="target"
■ Tells where to open the page sent as a result of the request
■ target= _blank means open in a new window
■ target= _top means use the same window
● The action attribute in the form element in line 20 specifies the URL of a script
on the web server that will be invoked to process the form’s data. Since we
haven’t introduced server-side programming yet, we set this attribute to
http://www.deitel.com for now.
● Lines 23–41 define input elements that specify data to provide to the script that
processes the form (also called the form handler).
● There are several types of input elements.
● An input’s type is determined by its type attribute.
● This form uses a text input, a submit input, a reset input and three hidden
inputs.
● Most, but not all, form elements use the <input> tag, with a type="..." attribute
to tell which kind of element it is
○ type can be text, checkbox, radio, password, hidden, submit, reset, button,
file, or image
● Other common input tag attributes include:
○ name: the name of the element
○ id: a unique identifier for the element
○ value: the “value” of the element; used in different ways for different values
of type
○ readonly: the value cannot be changed
○ disabled: the user can’t do anything with this element
○ Other arguments are defined for the input tag but have meaning only for
certain values of type
Web Systems [502315-3] Dr. Afnan Bukhari 17
6. text input Element
text input Element
A text field:
<input type="text" name="textfield" value="with an initial value" />
A password field:
<input type="password" name="textfield3" value="secret" />
• Note that two of these use the input tag, but one uses textarea
Web Systems [502315-3] Dr. Afnan Bukhari 19
text input Element
31 <p><label>Name:
32 <input name="name" type="text" size="25"
33 maxlength="30">
34 </label></p>
● The text input in lines 32–33 inserts a text field in the form.
● Users can type data in text fields.
● The label element (lines 31–34) provides users with information about the input
element’s purpose.
● The input element’s size attribute specifies the number of characters visible in the text
field.
● Optional attribute maxlength limits the number of characters input into the text field—in
this case, the user is not permitted to type more than 30 characters.
● readonly - a read-only input field cannot be modified.
● disabled - a disabled input element is unusable and un-clickable.
● A form will still submit an input field that is readonly, but will not submit an input field that
is disabled!
● The submit input element is a button. When the submit button is pressed, the
form’s data is sent to the location specified in the form’s action attribute.
● The value attribute sets the text displayed on the button.
● The reset input element allows a user to reset all form elements to their
default values.
● The value attribute of the reset input element sets the text displayed on the
button (the default value is Reset if you omit the value attribute).
40 <input type="submit" value="Submit">
41 <input type="reset" value="Clear">
textarea element
● The textarea element (lines 31–32) inserts a multiline text area into the form.
● The number of rows is specified with the rows attribute, and the number of
columns (i.e., characters per line) with the cols attribute.
● In this example, the textarea is four rows high and 36 characters wide.
● To display default text in the textarea, place the text between the
<textarea> and </textarea> tags. Default text can be specified in other
input types, such as text fields, by using the value attribute.
● The password input in line 39 inserts a password box with the specified size
(maximum number of displayed characters).
● A password box allows users to enter sensitive information, such as credit card
numbers and passwords, by “masking” the information input with asterisks (*).
● The actual value input is sent to the web server, not the masking characters.
● A checkbox:
<input type="checkbox" name="checkbox” value="checkbox" checked="checked">
type: "checkbox"
name: used to reference this form element from JavaScript
value: value to be returned when element is checked
Note that there is no text associated with the checkbox
Unless you use a label tag, only clicking on the box itself has any effect
Radio buttons:<br>
<input type="radio" name="radiobutton" value="myValue1" />
male<br>
<input type="radio" name="radiobutton" value="myValue2”
checked="checked" />female
• If two or more radio buttons have the same name, the user can only select one
of them at a time
• This is how you make a radio button “group”
• If you ask for the value of that name, you will get the value specified for the
selected radio button
• As with checkboxes, radio buttons do not contain any text
● Lines 69–83 introduce the radio button specified with type radio.
● radio buttons are similar to checkboxes, except that only one radio button in a
group of radio buttons may be selected at any time.
● The radio buttons in a group all have the same name attributes and are
distinguished by their different value attributes.
● The attribute checked (line 71) indicates which radio button, if any, is selected
initially. The checked attribute also applies to checkboxes.
● A menu or list:
<select name="select">
<option value="red">red</option>
<option value="green">green</option>
<option value=“blue">blue</option>
</select>
• Additional arguments:
• size: the number of items visible in the list (default is "1")
• multiple
• if set to "true" (or just about anything else), any number of items may be selected
• if omitted, only one item may be selected
• if set to "false", behavior depends on the particular browser
● The select element (lines 92–105) provides a drop-down list from which the user
can select an item.
● The name attribute identifies the drop-down list.
● The option elements (lines 93–104) add items to the drop-down list.
● The option element’s selected attribute specifies which item initially is
displayed as the selected item in the select element.
● If no option element is marked as selected, the browser selects the first
option by default.
38