~~SLIDESHOW~~ ====== Forms and Web User Interfaces ====== **Contact Hour 4**: To be discussed on Tuesday 5th February, 2013. **Lecturer**: [[C.P.Jobling@Swansea.ac.uk|Dr Chris P. Jobling]]. Forms are the most common way to communicate from a browser to a web server. They are also created using HTML tags. ---- These notes are based on Sections 2.9 and 2.11 of Robert W. Sebasta, //Programming the World-Wide Web//, 3rd Edition, Addison Wesley, 2006. This material is expanded in Chapter 3 of Nigel Chapman and Jenny Chapman, //Web Design: A complete introduction//, Wiley, 2006. Some additional attributes that have been added in HTML5 have been added to this discussion this year. ===== Forms and Web User Interfaces ===== * In this lecture we complete our revision of the most commonly used subset of the Hypertext Markup Language (HTML). * We look at forms which are the most common way to communicate information from a user to a web server. * Forms are the user-interface of web applications. * As before, examples are on-line and code is reproduced in the handout. * The new form elements added by HTML5 will be discussed in a later session. ---- Further reading. E.g.: A different treatment of HTML given in Chapter 3 of Chapman and Chapman, //Web Design: a complete introduction//, John wiley, 2006 and Part I (Chapters 2-3) of the textbook Chris Bates, //Web Programming: Building Internet Applications//, 3rd Edition, John Wiley, 2006. However, you should note that Bates tends to use the simpler form of HTML which is perfectly valid, but doesn't match the stricter conventions we are used to from our study of XHTML. You will find additional information about web forms and further examples in Chapter 14 of Elisabeth Freeman and Eric Freeman, //Head First HTML with CSS & XHTML//, O'Reilly Media,Inc., 2006. Information about the new attributes and new form elements are to be found in [[http://diveintohtml5.info/forms.html|Chapter 9 A Form of Madness]] of [[http://diveintohtml5.info|Dive Into HTML5]]. ===== About the Examples ===== Before these examples will work you will need to install the EG-259 Virtual Machine. See the instructions at [[https://github.com/cpjobling/eg-259-vm]]. * Go directly to the examples: http://localhost:4567/eg-259/examples/lecture3 * Browse the source code on GitHub: [[https://github.com/cpjobling/eg-259-vm/tree/master/web/eg-259/examples/lecture3|eg-259/examples/lecture3]] ===== Contents ===== User interfaces for Web Applications. * [[eg-259:lecture3#forms|Forms]] * [[eg-259:lecture3#user_interface_components_widgets|User Interface Components (Widgets)]] * [[eg-259:lecture3#useful_attributes_added_by_html5|Useful Attributes Added by HTML5]] * [[eg-259:lecture3#two_examples|Two Examples]] ===== Learning Outcomes ==== Consider converting these into multiple choice questions for the PeerWise exercise. //At the end of this lecture you should be able to answer these questions//: - What are controls? - Which controls discussed in this session are created with the '''' tag? - What is the default size of a text control's text box? - What is the difference between the ''size'' and ''maxlength'' attributes of '''' for text controls? ===== Learning Outcomes – Forms (continued) ===== //At the end of this lecture you should be able to answer these questions//: - What is the difference in behaviour between a group of check box buttons and a group of radio buttons? - Under what circumstances is a menu used instead of a radio button group? - What is the drawback of specifying the ''multiple'' attribute with a menu? - How are scroll bars specified for text-area controls? ===== Learning Outcomes – Forms (continued) ===== //At the end of this lecture you should be able to answer these questions//: - What does the new HTML5 attribute ''placeholder'' do? - What is the new HTML5 attribute ''autofocus'' for? - What is the new HTML5 attribute ''required'' for? - Why is the choice of ''method="get"'' as the default for the form element a bad thing? ===== Forms ===== * A form is the usual way information is gotten from a user to a web application * HTML has tags to create a collection of objects that implement this information gathering * The objects are called //controls// or //widgets// (e.g., radio buttons and checkboxes) * When the //submit// button of a form is clicked, the form's values are collected by the browser and are sent to the server in an HTTP request. ===== The Form Element ===== * The ''
'' element is a container for a group of form widgets. * All of the widgets must be defined in the content of a '''' tag * There can be any number of independent forms on a single web page ===== Form — action attribute ===== * The only required attribute of '''' is ''action'', which specifies the URI of the application that is to be called when the //Submit// button is clicked: action = "http://localhost:4567/cgi-bin/echo_params.cgi" * If the form has no ''action'' attribute, the value of ''action'' is the empty string ===== Form — method attribute ===== * The ''method'' attribute of '''' specifies one of the two possible techniques of transferring the form data to the server, ''get'' and ''post''((Early drafts of the HTML5 added ''put'' and ''delete'' but these have been removed until more is known abut how these verbs should be implemented by browsers. See [[https://www.w3.org/Bugs/Public/show_bug.cgi?id=10671|HTML5 Bug 10671]].)) * The actions of the ''get'' and ''post'' methods are discussed in great detail later in the module. * The ''get'' method is the default ---- The fact that the ''get'' method is the default is not necessarily a good thing! ===== User Interface Components (Widgets) ===== * HTML4/XHTML 1.0 has a relatively small set of user interface components * Most are created with the '''' tag * The type attribute of '''' specifies the kind of user input component or widget being created * HTML5 adds more input types and features, but browser support for these is patchy. ---- We will consider the full set of HTML5 form elements in a later session. ===== Widget types ===== * The main types of widget are: * [[eg-259:lecture3#text_widgets|Text]] * [[eg-259:lecture3#check-box_widgets|Checkboxes]] * [[eg-259:lecture3#radio_button_widgets|Radio buttons]] * [[eg-259:lecture3#menus|Menus and menu items]] * [[eg-259:lecture3#text_area_widget|Text area]] * [[eg-259:lecture3#reset_and_submit_buttons|Submit and reset buttons]] ===== Text Widgets ===== **''''** * Creates a horizontal box for text input * Default size is 20 (characters); it can be changed with the ''size'' attribute * If more characters are entered than will fit, the box is scrolled (shifted) left * If you don't want to allow the user to type more characters than will fit, set ''maxlength'', which causes excess input to be ignored ===== An example =====

---- Note the ''label'' element is used to define a label. The value of the attribute ''for'' should be an id reference. The label is linked to the form element with the matching id. It is quite common practice to give form elements an id because this can be used by JavaScript to identify the form element that fires an event, or as in this case, to relate the label to the corresponding form element. This is a useful accessibility feature. You might want to look at the remaining examples in this session to see if label is not more appropriate than the plain text used to label form widgets. See the formal definition of [[http://dev.w3.org/html5/markup/label.html|label]] ===== Check-box Widgets ===== **''''** * Check-boxes are used to collect multiple choice input * Every check-box requires a ''value'' attribute, which is the widget's value in the form data when the check-box is 'checked' * A check-box that is not 'checked' contributes no value to the form data * By default, no check-box is initially 'checked' * To initialize a check-box to 'checked', the ''checked'' attribute must be present((In XHTML this must be ''checked="checked"''.)) ===== Check-Box Widgets (Example) ===== Grocery Checklist: [[http://localhost:4567/eg-259/examples/lecture3/checkbox.html|checkbox.html]]

Grocery Checklist

Milk Bread Eggs

---- **//Code for the Example//** Checkboxes

Grocery Checklist

Milk Bread Eggs

===== Radio Button Widgets ===== **''''** * Radio buttons are collections of check-boxes in which only one button can be 'checked' at a time * Every button in a radio button group **MUST** have the same name * If no button in a radio button group is 'pressed', the browser often 'presses' the first one ===== Radio Button Widgets (Example) ===== * Age category: [[http://localhost:4567/eg-259/examples/lecture3/radio.html|radio.html]]

Age Category

0-19 20-35 36-50 Over 50

---- Radio

Age Category

0-19 20-35 36-50 Over 50

===== Menus ===== **'''' tags * There are two kinds of menus, those that behave like check-boxes and those that behave like radio buttons (the default) * Menus that behave like check-boxes are specified by including the ''multiple'' attribute((which must be set to ''multiple="multiple"'' in XHTML)) * The ''name'' attribute of '''' can be included to specify the number of menu items to be displayed (the default is 1) * If ''size'' is set to > 1 or if ''multiple'' is specified, the menu is displayed as a pop-up menu ===== Menu Items ===== **''