html forms lecture 19 - hampden-sydney...

35
HTML Forms Lecture 19 Robb T. Koether Hampden-Sydney College Wed, Feb 29, 2012 Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 1 / 35

Upload: others

Post on 09-Aug-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

HTML FormsLecture 19

Robb T. Koether

Hampden-Sydney College

Wed, Feb 29, 2012

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 1 / 35

Page 2: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

1 HTML Forms

2 Special Buttons

3 The input ElementText BoxesPasswordsRadio ButtonsCheck Boxes

4 Text Areas

5 Select Lists

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 2 / 35

Page 3: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

Outline

1 HTML Forms

2 Special Buttons

3 The input ElementText BoxesPasswordsRadio ButtonsCheck Boxes

4 Text Areas

5 Select Lists

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 3 / 35

Page 4: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

HTML Forms

An HTML form element allows the client to enter data, which isthen transmitted, typically, to a PHP program.We use the tags <form>...</form>.

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 4 / 35

Page 5: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

HTML Forms

Within the form tag, we need to include at least the following twoattributes.

method - specifies how to transmit the data.action - specifies what file to invoke when the form is submitted.

We may also include other attributes.name - used for reference.onsubmit - invokes a Javascript function when the form issubmitted.

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 5 / 35

Page 6: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

HTML Forms

Forms<form name="myform" method="GET" action="myaction.php">

:</form>

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 6 / 35

Page 7: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

The method Attribute

The method attribute specifies either GET or POST.Both choices will transmit the form values to the action file, but byslightly different means.

GET - The values are appended to the name of the invoked actionfile, visible in the URL.POST - The values are transmitted “quietly.”

The details will be described shortly.

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 7 / 35

Page 8: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

HTML Forms

Within the form tags, we may use a variety of methods by whichthe user may provide data.

ButtonsText boxesRadio buttonsCheck boxesSelect listsText areas

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 8 / 35

Page 9: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

Outline

1 HTML Forms

2 Special Buttons

3 The input ElementText BoxesPasswordsRadio ButtonsCheck Boxes

4 Text Areas

5 Select Lists

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 9 / 35

Page 10: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

Submit Button

Submit Button<form method="GET" action="myaction.php">

:<input type="submit" name="Submit"/>

</form>

This creates a standard button labeled “Submit”. (The defaultlabel is “Submit Query”.)The form data are transmitted to the file myaction.php when theSubmit button is pressed.

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 10 / 35

Page 11: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

Reset Button

Submit Button<form method="GET" action="home.php">

<input type="reset" name="Reset"/></form>

This creates a standard button labeled “Reset”. (The default labelis “Reset”.)When the Reset button is pressed, all values are given their initialvalues, as defined by the HTML code.

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 11 / 35

Page 12: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

Outline

1 HTML Forms

2 Special Buttons

3 The input ElementText BoxesPasswordsRadio ButtonsCheck Boxes

4 Text Areas

5 Select Lists

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 12 / 35

Page 13: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

The input Element

Text Boxes<input type="type-of-input" name="element-name"/>

An input element allows various forms of data entry, dependingon its type.It needs type and name attributes.The input tag is self-closing.The type of input maybe submit, reset, text, password,radio, or checkbox.

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 13 / 35

Page 14: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

Outline

1 HTML Forms

2 Special Buttons

3 The input ElementText BoxesPasswordsRadio ButtonsCheck Boxes

4 Text Areas

5 Select Lists

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 14 / 35

Page 15: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

The text Type

Text BoxesEnter username:<input type="text" name="username"/>

The above example displays the text Enter username: with atext box after it.The user may enter his username in the text box.When he clicks the Submit button, his username is transmitted tothe action file.

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 15 / 35

Page 16: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

The text Type

Suppose that the user enters John Doe in the text box.When the user submits the form, the form will create the URL

myaction.php?username=John+Doe

and give it to the browser.

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 16 / 35

Page 17: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

The text Type

In the file myaction.php, the username will be received throughthe associative array $_GET, where

$_GET[’username’] = "John Doe"

The POST method is similar, except thatThe values are not attached to the filename, so they do not appearin the URL.The associative array is $_POST, not $_GET.

$_POST[’username’] = "John Doe"

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 17 / 35

Page 18: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

Outline

1 HTML Forms

2 Special Buttons

3 The input ElementText BoxesPasswordsRadio ButtonsCheck Boxes

4 Text Areas

5 Select Lists

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 18 / 35

Page 19: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

The password Type

Passwords<form method="POST" action="myaction.php">

Enter password:<input type="password" name="password"/>

</form>

If the type is password rather than text, then the behavior is thesame, except that the plain text is hidden as it is typed.For example, if the user types hampden, all that will appear is•••••••.

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 19 / 35

Page 20: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

Outline

1 HTML Forms

2 Special Buttons

3 The input ElementText BoxesPasswordsRadio ButtonsCheck Boxes

4 Text Areas

5 Select Lists

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 20 / 35

Page 21: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

The radio Type

The radio button needs type, name, and value attributes.All radio buttons in a group use the same name, but differentvalues.Only one button can be “on” at a time.

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 21 / 35

Page 22: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

The radio Type

Radio ButtonsWhat size do you want?<input type="radio" name="size" value="small"/> Small<input type="radio" name="size" value="medium"/> Medium<input type="radio" name="size" value="large"/> Large

The user may select “Small,” “Medium,” or “Large.”

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 22 / 35

Page 23: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

The radio Type

Radio Buttons$_GET[’size’] = "medium"

When the user submits the form, the name of the transmitted attribute issize and its value is whichever button was last selected.

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 23 / 35

Page 24: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

The radio Type

Radio ButtonsWhat size do you want?<input type="radio" name="size" value="small"

checked="checked"/> Small<input type="radio" name="size" value="medium"/> Medium<input type="radio" name="size" value="large"/> Large

We may pre-select a category.

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 24 / 35

Page 25: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

Outline

1 HTML Forms

2 Special Buttons

3 The input ElementText BoxesPasswordsRadio ButtonsCheck Boxes

4 Text Areas

5 Select Lists

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 25 / 35

Page 26: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

The checkbox Type

Check boxes are similar to radio buttons, except that the user maycheck any combination of boxes, including none.The check box needs type, name, and value attributes.Because the choices are independent, checkboxes are notlogically grouped.Therefore, each box has its own name.

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 26 / 35

Page 27: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

The checkbox Type

Check BoxesWhich sports do you play?<input type="checkbox" name="sport1"/> Basketball<input type="checkbox" name="sport2"/> Football<input type="checkbox" name="sport3"/> Baseball

Each selected item is given the default value on.

All other items are undefined.

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 27 / 35

Page 28: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

The checkbox Type

Check BoxesWhich sports do you play?<input type="checkbox" name="sport1" value="bskt"/> Basketball<input type="checkbox" name="sport2" value="foot"/> Football<input type="checkbox" name="sport3" value="base"/> Baseball

We may assign other values instead of “on.”

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 28 / 35

Page 29: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

The checkbox Type

Check BoxesWhich sports do you play?<input type="checkbox" name="sport[]" value="bskt"/> Basketball<input type="checkbox" name="sport[]" value="foot"/> Footbal<input type="checkbox" name="sport[]" value="base"/> Baseball

It is possible to use one array name for the group of check boxes.In this case, $_POST[’sport’] or $_GET[’sport’] is anarray.Only those values that are checked are placed in the array.In this case it is necessary to specify the value for each checkbox.

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 29 / 35

Page 30: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

Outline

1 HTML Forms

2 Special Buttons

3 The input ElementText BoxesPasswordsRadio ButtonsCheck Boxes

4 Text Areas

5 Select Lists

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 30 / 35

Page 31: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

The password Tag

Text Areas<textarea name="msg">Enter message here</textarea>

For larger quantities of text, we can use a textarea element.A textarea element creates a rectangle in which we may entermulti-line text.The textarea element can set the size of the box by using theattributes rows and cols.The text between the tags will appear in the rectangle.If the text exceeds the size of the box, a scroll bar will appear.

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 31 / 35

Page 32: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

Outline

1 HTML Forms

2 Special Buttons

3 The input ElementText BoxesPasswordsRadio ButtonsCheck Boxes

4 Text Areas

5 Select Lists

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 32 / 35

Page 33: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

The select Tag

Select List<select name="color"><option value="red">Red</option><option value="yellow">Yellow</option><option value="green">Green</option><option value="blue">Blue</option></select>

A select element allows the user to select from a pre-defined listof choices.Each choice is defined by an option element within the selecttags.This creates a pop-up menu from which the user can make aselection.

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 33 / 35

Page 34: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

The select Tag

Select List<select name="color"><option value="red">Red</option><option value="yellow">Yellow</option><option value="green" selected="selected">Green</option><option value="blue">Blue</option></select>

One of the options may be designated “selected.”In this example, the “green” option will appear selected when thepage is loaded.

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 34 / 35

Page 35: HTML Forms Lecture 19 - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2012/L… · HTML Forms An HTML form element allows the client to enter data,

Multiple Selections

Select List<select name="color[]" multiple="multiple" size="3"><option value="red">Red</option><option value="yellow">Yellow</option><option value="green" selected="selected">Green</option><option value="blue">Blue</option></select>

With the multiple attribute, we can make multiple selections.If we use the multiple attribute, then we should make the nameof the select element an array.With the size attribute, we can specify how many options are inview at one time.

Robb T. Koether (Hampden-Sydney College) HTML FormsLecture 19 Wed, Feb 29, 2012 35 / 35