HTML ch10 HTML Forms
HTML ch10 HTML Forms
Html forms
HTML Forms
action Backend script ready to process your passed data. The action attribute defines
the action to be performed when the form is submitted.
method Method to be used to upload data. The most frequently used are GET and POST
methods.
target Specify the target window or frame where the result of the script will be displayed. It
takes values like _blank, _self, _parent etc.
enctype You can use the enctype attribute to specify how the browser encodes the data before
it sends it to the server. Possible values are:
•application/x-www-form-urlencoded - This is the standard method most forms use in
simple scenarios.
•mutlipart/form-data - This is used when you want to upload binary data in the form of
files like image, word file etc.
Form methods
• The method attribute specifies the HTTP
method (GET or POST) to be used when
submitting the form data:
• The default method when submitting form
data is GET.
• However, when GET is used, the submitted
form data will be visible in the page address
field:
GET method
<p>After you submit, notice that the form values is visible in the address bar of the new browser
tab.</p>
</body>
</html>
Post
• Always use POST if the form data contains
sensitive or personal information. The POST
method does not display the submitted form
data in the page address field.
• POST has no size limitations, and can be used
to send large amounts of data.
• Form submissions with POST cannot be
bookmarked
Post example
<!DOCTYPE html>
<html>
<body>
<p>After you submit, notice that, unlike the GET method, the form values is NOT visible in the address bar of the new
browser tab.</p>
</body>
</html>
HTML Form Controls
• There are different types of form controls that
you can use to collect data using HTML form:
Adding text
• Text Input Controls text input
• Checkboxes Controls Password input
• Radio Box Controls Multi line text
• Select Box Controls Choices
• File Select boxes Radio buttons
Check boxes
• Hidden Controls
Drop down boxes(Select boxes)
• Clickable Buttons Submitting forms
• Submit and Reset Button File select boxes
Clickable buttons
Submit button
Image button
Upload button
Text Input Controls
Attribute Description
type Indicates the type of input control and for text input control it will be set
to text.
name Used to give a name to the control which is sent to the server to be
recognized and get the value.
value This can be used to provide an initial value inside the control.
size Allows to specify the width of the text-input control in terms of characters.
maxlength Allows to specify the maximum number of characters a user can enter
into the text box.
Password input controls
Attribute Description
type Indicates the type of input control and for password
input control it will be set to password.
name Used to give a name to the control which is sent to the
server to be recognized and get the value.
value This can be used to provide an initial value inside the
control.
size Allows to specify the width of the text-input control in
terms of characters.
maxlength Allows to specify the maximum number of characters
a user can enter into the text box.
Multiple-Line Text Input Controls
Attribute Description
name Used to give a name to the control
which is sent to the server to be
recognized and get the value.
rows Indicates the number of rows of text
area box.
cols Indicates the number of columns of text
area box
Grouping elements
• Grouping Form Data with <fieldset>
• The <fieldset> element is used to group
related data in a form.
• The <legend> element defines a caption for
the <fieldset> element.
Fieldset example
• <!DOCTYPE html>
• <html>
• <body>
• <form action="/action_page.php">
• <fieldset>
• <legend>Personal information:</legend>
• First name:<br>
• <input type="text" name="firstname" value="Mickey">
• <br>
• Last name:<br>
• <input type="text" name="lastname" value="Mouse">
• <br><br>
• <input type="submit" value="Submit">
• </fieldset>
• </form>
• </body>
• </html>
Checkbox Control
Attribute Description
type Indicates the type of input control and for checkbox input
control it will be set to checkbox.
Attribute Description
type Indicates the type of input
control and for checkbox input
control it will be set to radio.
name Used to give a name to the
control which is sent to the
server to be recognized and get
the value.
value The value that will be used if the
radio box is selected.
checked Set to checked if you want to
select it by default.
The select element
• The <select> Element
• The <select> element defines a drop-down list
• The <option> elements defines an option that
can be selected.
• 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:
Select Example 1
• <!DOCTYPE html>
• <html>
• <head>
• <title>Select Box Control</title>
• </head>
• <body>
• <form>
• <select name="dropdown"> <option value="Maths"
selected>Maths</option>
• <option value="Physics">Physics</option> </select> </form>
• </body>
• </html>
Select Example 2
• <!DOCTYPE html>
• <html>
• <body>
• <form action="/action_page.php">
• <label for="cars">Choose a car:</label>
• <select id="cars" name="cars">
• <option value="volvo">Volvo</option>
• <option value="saab">Saab</option>
• <option value="fiat">Fiat</option>
• <option value="audi">Audi</option>
• </select>
• <input type="submit">
• </form>
• </body>
• </html>
Attributes
Following is the list of important attributes of <select> tag:
Attribute Description
Attribute Description
Attribute Description
Type Description
submit This creates a button that automatically submits a form.
reset This creates a button that automatically resets form controls to their
initial values.
button This creates a button that is used to trigger a client-side script when
the user clicks that button.
<form action="/action_page.php">
<input list="browsers" name="browser">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
<input type="submit">
</form>
<p><b>Note:</b> The datalist tag is not supported in Safari or IE9 (and earlier).</p>
</body>
</html>
HTML5 <output> Element
<form action="/action_page.php"
oninput="x.value=parseInt(a.value)+ parseInt(b.value)"> 0
<input type="range" id="a" name="a" value="50"> 100 +
<input type="number" id="b" name="b" value="50">
= <output name="x" for="a b"></output>
<br><br>
<input type="submit">
</form>
<p><strong>Note:</strong> The output element is not supported in Edge 12 or Internet Explorer
and earlier versions.</p>
</body>
</html>
HTML5 Input Types