Professional Documents
Culture Documents
Lec. 6 HTML Forms
Lec. 6 HTML Forms
Zahraa
Ayad
Internet Programming
Lecture 6
HTML Forms, Styling Forms
14 November 2023 1
by Asst. Lect. Zahraa
Ayad
Outlines
HTML Forms Drop Down List Box (Select
Box)
Form Controls
File Upload Box
How Form Works?
Submit Button
Form Structure
Buttons
Text Input
Labeling Form Controls
Password Input
Grouping Form Elements
Text Area
Form Validation
Radio Buttons
Form Examples
Checkbox
Styling Forms
14 November 2023 2
by Asst. Lect. Zahraa
Ayad
HTML Forms
An HTML form is used to collect user input. The user input is most often sent to a server
for processing.
The best known form on the web is probably the search box that sits right in the middle of
Google's homepage.
In addition to enabling users to search, forms also allow users to perform other functions
online. You will see forms when registering as a member of a website, when shopping
online, and when signing up for newsletters or mailing lists.
14 November 2023 3
by Asst. Lect. Zahraa
Ayad
14 November 2023 4
by Asst. Lect. Zahraa
Ayad
Form Controls
There are several types of form controls that you can use to collect information
from visitors to your site:
Adding Text
Text input (single-line) Used for a single line of text such as
email addresses and names.
Like a single line text box but it masks the
Password input
characters entered.
14 November 2023 5
by Asst. Lect. Zahraa
Ayad
Uploading Files
Allows users to upload files (e.g. images)
File upload
to a website.
14 November 2023 6
by Asst. Lect. Zahraa
Ayad
14 November 2023 7
by Asst. Lect. Zahraa
Ayad
14 November 2023 8
by Asst. Lect. Zahraa
Ayad
Form Structure
<form> Element
Form controls live inside a <form> element. This element should always carry the
action attribute and will usually have a method and id attribute too.
Attributes:
14 November 2023 9
by Asst. Lect. Zahraa
Ayad
Text Input
<input> Element
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.
Attributes:
14 November 2023 10
by Asst. Lect. Zahraa
Ayad
Password Input
By using the <input> Element
Attributes:
14 November 2023 11
by Asst. Lect. Zahraa
Ayad
Text Area
<textarea> Element
• The <textarea> element is used to create a mutli-line text input.
• 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.
Attributes:
Attribute Name Description
name Specifies the name of an <textarea> element to identify the form
control
Example:
14 November 2023 12
by Asst. Lect. Zahraa
Ayad
Radio Buttons
By using the <input> Element
Attributes:
Attribute Name Description
type (type=“radio”) Radio buttons allow users to pick just one of a number of
options.
name Specifies the name of an <input> element to identify the form control
(should be the same for all radio buttons)
value The value attribute indicates the value that is sent to the server for the
selected option.
checked (Boolean attribute) can be used to indicate which value (if any) should be
selected when the page loads.
Example:
14 November 2023 13
by Asst. Lect. Zahraa
Ayad
Checkbox
By using the <input> Element
Attributes:
Attribute Name Description
type (type=“checkbox”) Checkboxes allow users to select (and unselect) one or
more options in answer to a question.
name Specifies the name of an <input> element to identify the form control
value The value attribute indicates the value that is sent to the server if this
checkbox is checked
checked (Boolean attribute) The checked attribute indicates that this box should be
checked when the page loads.
Example:
14 November 2023 14
by Asst. Lect. Zahraa
Ayad
Example:
14 November 2023 15
by Asst. Lect. Zahraa
Ayad
Example:
14 November 2023 16
by Asst. Lect. Zahraa
Ayad
Attributes:
Example:
14 November 2023 17
by Asst. Lect. Zahraa
Ayad
Submit button
By using the <input> Element
Attributes:
Attribute Name Description
type (type=“submit”) creates a button that is used to send a form to the server.
value is used to control the text that appears on a button. (the default value is
submit query)
Example:
14 November 2023 18
by Asst. Lect. Zahraa
Ayad
Buttons
We can add buttons to our form using the <button> element
Attributes:
Attribute Name Description
type (type=“submit”) used to send a form to the server.
(type=“reset”) resets all the controls to their initial values
Example:
14 November 2023 19
by Asst. Lect. Zahraa
Ayad
Example:
14 November 2023 20
by Asst. Lect. Zahraa
Ayad
<legend> Element
The <legend> element can come directly after the opening <f ie ldset> tag and
contains a caption which helps identify the purpose of that group of form controls.
Example:
14 November 2023 21
by Asst. Lect. Zahraa
Ayad
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 (valid information).
• HTML5 comes with built-in validation. The most common validation attributes are
required, minlength, maxlength, min, and max.
14 November 2023 22
by Asst. Lect. Zahraa
Ayad
For numbers
14 November 2023 23
by Asst. Lect. Zahraa
Ayad
14 November 2023 24
by Asst. Lect. Zahraa
Ayad
Form Examples
Example 1:
14 November 2023 25
by Asst. Lect. Zahraa
Ayad
Form Examples
Output:
14 November 2023 26
by Asst. Lect. Zahraa
Ayad
Form Examples
Example 2:
14 November 2023 27
by Asst. Lect. Zahraa
Ayad
Form Examples
Output :
14 November 2023 28
by Asst. Lect. Zahraa
Ayad
Styling Forms
We can change the style of form controls by using the following CSS properties:
14 November 2023 29
by Asst. Lect. Zahraa
Ayad
14 November 2023 30
by Asst. Lect. Zahraa
Ayad
14 November 2023 31
by Asst. Lect. Zahraa
Ayad
14 November 2023 32
by Asst. Lect. Zahraa
Ayad
14 November 2023 33
by Asst. Lect. Zahraa
Ayad
14 November 2023 34
by Asst. Lect. Zahraa
Ayad
14 November 2023 35
by Asst. Lect. Zahraa
Ayad
End of Lecture 6
14 November 2023 36