5. SIMPLE FORM: CONTACT US < form id = "contact-form" action = "/action-page.php" method = "post" > <!-- the rest of our form will go here --> </ form > FORM Element establishes a form ACTION is the only required attribute and should always be a URI METHOD defaults to “get” NAME is depreciated; use ID instead
6. SIMPLE FORM: CONTACT US <form id="contact-form" action="/action-page.php" method="post"> < fieldset > <legend> Send us a message </legend> <!-- the rest of our form will go here --> </ fieldset > </form> FIEDSET Element used to group related fields LEGEND Element used to provide a caption for a FIELDSET
7. SIMPLE FORM: CONTACT US <form id="contact-form" action="/action-page.php" method="post"> <fieldset> <legend> Send us a message </legend> < p > <!-- form control --> </ p > < p > <!-- form control --> </ p > < p > <!-- form control --> </ p > </fieldset> </form> Containing FORM Controls P or DIV sensible choices, but not very accurate (except in certain instances) OL or UL most forms are lists of questions or form controls, so these are better <form id="contact-form" action="/action-page.php" method="post"> <fieldset> <legend> Send us a message </legend> < ol > < li > <!-- form control --> </ li > < li > <!-- form control --> </ li > < li > <!-- form control --> </ li > </ ol > </fieldset> </form>
8. SIMPLE FORM: CONTACT US <form id="contact-form" action="#" method="post"> <fieldset> <legend>Send us a message</legend> <ol> <li> Name < input type = "text" name = "name" id = "contact-name" /> </li> <li> Email < input type = "text" name = "email" id = "contact-email" /> </li> <li> <!-- form control --> </li> </ol> </fieldset> </form> INPUT Text Control type= "name" is a basic text input field (also type= "password" for content you want hidden) NAME vs. ID NAME is for the back end ID is for the front end
9. SIMPLE FORM: CONTACT US <form id="contact-form" action="#" method="post"> <fieldset> <legend>Send us a message</legend> <ol> <li>Name <input type="text" name="name" id="contact-name" /></li> <li>Email <input type="text" name="email" id="contact-email" /></li> <li> Message < textarea name = "message" id = "contact-message" rows = "4" cols = "30" > </ textarea > </li> </ol> </fieldset> </form> TEXTAREA a multiline text form control requires ROWS and COLS attributes!!!
10. SIMPLE FORM: CONTACT US Working with LABEL this element provides to means of associating its content with a form control: implicit association LABEL wraps the form control and the text explicit association LABEL 's FOR attribute is an ID reference to the form control <form id="contact-form" action="/action-page.php" method="post"> <fieldset> <legend> Send us a message </legend> <ol> <li> < label for = "contact-name" > Name </ label> < input id = "contact-name" ... /> </li> ... </ol> </fieldset> </form> <form id="contact-form" action="/action-page.php" method="post"> <fieldset> <legend> Send us a message </legend> <ol> <li> < label > Name < input ... /></ label> </li> ... </ol> </fieldset> </form>
11. SIMPLE FORM: CONTACT US Buttons trigger events in a form; use either INPUT or BUTTON element Common TYPE s submit – submits the form; default button type reset – resets all form control values back to their defaults when the page loaded <form id="contact-form" action="#" method="post"> <fieldset> <legend>Send us a message</legend> <ol> ... </ol> <button type = "submit" > Go </button> </fieldset> </form> <form id="contact-form" action="#" method="post"> <fieldset> <legend>Send us a message</legend> <ol> ... </ol> <input type = "submit" value = "Go" / > </fieldset> </form>
12. SIDEBAR : BUTTONS WINDOWS XP OS X BUTTON INPUT Mozilla IE 6/7 (XP) IE 6/7 (classic) Opera Opera IE 5 Firefox Camino Safari
22. SIMPLE FORM: CONTACT US SELECT ion Lists allows selection of one or more OPTION s On OPTION elements, the VALUE attribute is optional (contents are submitted by default) <form id="contact-form" action="#" method="post"> <fieldset> <legend>Send us a message</legend> <ol> ... <li>< label for = "contact-subject" > Subject </ label > < select id = "contact-subject" name = "subject" > < option value = "Error" > I noticed a website error </ option > < option value = "Question" > I have a question </ option > < option > Other </ option > </ select ></li> ... </ol> <button type="submit">Go</button> </fieldset> </form>
28. SIMPLE FORM: CONTACT US Nested FIELDSET s a great way to organize radio or checkbox groups The LEGEND is the question or statement Lists organize the possible responses ( OL or UL ) implicit LABEL s provide an easy way to style in IE6 ... < li > < fieldset class = "radio" > < legend > I would prefer to be contacted by </ legend > < ul > < li >< label >< input type = "radio" name = "method" value = "email" /> email </ label ></ li > < li >< label >< input type = "radio" name = "method" value = "phone" /> phone </ label ></ li > </ ul > </ fieldset > </ li > ...
29. SIMPLE FORM: CONTACT US <form id="contact-form" action="#" method="post"> ... <li> <fieldset class="radio"> <legend>I would prefer to be contacted by</legend> <ul> <li><label><input type="radio" name="method" value="email" /> email</label></li> <li><label><input type="radio" name="method" value="phone" /> phone</label></li> </ul> </fieldset> </li> ... </form>
36. SIMPLE FORM: CONTACT US <form id="contact-form" action="#" method="post"> <fieldset> <legend>Send us a message</legend> <ol> ... < li class = "confirm" > < input type = "hidden" name = "mailing-list" value = "0" /> < label >< input type = "checkbox" name = "mailing-list" value = "1" /> Please add me to your mailing list </ label ></ li > ... </ol> <button type="submit">Go</button> </fieldset> </form> Confirmations a little CLASS ification goes a long way
37. SIMPLE FORM: CONTACT US <form id="contact-form" action="#" method="post"> <fieldset> <legend>Send us a message</legend> <ol> ... <li class="confirm"> <input type="hidden" name="mailing-list" value="0" /> <label><input type="checkbox" name="mailing-list" value="1" /> Please add me to your mailing list</label></li> ... </ol> <button type="submit">Go</button> </fieldset> </form>
38. SIMPLE FORM: CONTACT US .confirm label { display: block; float: none; margin-left: 125px; text-align: left; width: 270px; } <form id="contact-form" action="#" method="post"> <fieldset> <legend>Send us a message</legend> <ol> ... <li class="confirm"> <input type="hidden" name="mailing-list" value="0" /> <label><input type="checkbox" name="mailing-list" value="1" /> Please add me to your mailing list</label></li> ... </ol> <button type="submit">Go</button> </fieldset> </form>
39. SIMPLE FORM: CONTACT US .confirm { margin-bottom: 1.4em; } .radio label:after , .confirm label:after { content: ''; } <form id="contact-form" action="#" method="post"> <fieldset> <legend>Send us a message</legend> <ol> ... <li class="confirm"> <input type="hidden" name="mailing-list" value="0" /> <label><input type="checkbox" name="mailing-list" value="1" /> Please add me to your mailing list</label></li> ... </ol> <button type="submit">Go</button> </fieldset> </form>
42. SIMPLE FORM: SEARCH BOX POST vs. GET Search forms are traditionally GET requests to allow the action page (i.e. the results) to be bookmarkable. < form id = "search-form" action = "/action-page.php" method = "get" > <!-- the rest of our form will go here --> </ form >
43. SIMPLE FORM: SEARCH BOX You need something Sometimes a FIELDSET is unnecessary, but in XHTML, you need something to wrap the contents of a form <form id="search-form" action="/action-page.php" method="get"> <p> <!-- the rest of our form will go here --> <p> </form>
44. SIMPLE FORM: SEARCH BOX Easy-peasy <form id="search-form" action="/action-page.php" method="get"> <p> < label for = "search-query" > Search this site for </ label > < input type = "text" id = "search-query" name = "query" /> <p> </form>
45. SIMPLE FORM: SEARCH BOX It’s a BUTTON big shock, I know <form id="search-form" action="/action-page.php" method="get"> <p> <label for="search-query">Search this site for</label> <input type="text" id="search-query" name="query" /> < button type = "submit" > Go </ button > <p> </form>
46. SIMPLE FORM: SEARCH BOX <form id="search-form" action="/action-page.php" method="get"> <p> <label for="search-query">Search this site for</label> <input type="text" id="search-query" name="query" /> <button type="submit">Go</button> <p> </form> body { background: #54af44; font: 62.5% "Lucida Sans Unicode", "Lucida Grande", sans-serif; } ol, ul, p { font-size: 1.2em; line-height: 1.5; }
50. SIMPLE FORM: DATE SELECT Getting organized < fieldset class = "date" > <!-- the rest will go here --> </ fieldset >
51. SIMPLE FORM: DATE SELECT Not really a LABEL <fieldset class="date"> < legend > Post Date </ legend > <!-- the rest will go here --> </fieldset>
52. SIMPLE FORM: DATE SELECT Not just a SELECT we need some LABEL ing <fieldset class="date"> <legend>Post Date</legend> < ol > < li > < label for = "date-day" > Date </ label > < select id = "date-day" name = "day" > < option > 01 </ option > ... < option > 31 </ option > </ select > </ li > </ ol > </fieldset>
53. SIMPLE FORM: DATE SELECT And so on <fieldset class="date"> <legend>Post Date</legend> <ol> <li> <label for="date-day">Date</label> ... </li> < li > < label for = "date-month" > Month </ label > < select id = "date-month" name = "month" > < option value = "01" > January </ option > ... < option value = "12" > December </ option > </ select > </ li > </ ol > </ fieldset >
54. SIMPLE FORM: DATE SELECT And so forth <fieldset class="date"> <legend>Post Date</legend> <ol> <li> <label for="date-day">Date</label> ... </li> <li> <label for="date-month">Month</label> ... </li> < li > < label for = "date-year" > Year </ label > < select id = "date-year" name = "year" > < option > 2007 </ option > < option > 2008 </ option > </ select > </ li > </ol> </fieldset>
63. COMPLEX FORM: RELATED FIELDS Organization and coordination as with other elements, form components can have multiple CLASS ifications < fieldset class = "radio related" > < legend > < span > Confine results to </ span > </ legend > < ul > < li > <!-- year --> </ li > < li > <!-- month --> </ li > < li > <!-- range --> </ li > </ ul > </ fieldset >
64. COMPLEX FORM: RELATED FIELDS Basic implicit LABEL nothing shocking here <fieldset class="radio related"> <legend><span>Confine results to</span></legend> <ul> <li> < label > < input type = "radio" name = "confines" value = "year" /> within the last year </ label > </li> ... </ul> </fieldset>
65. COMPLEX FORM: RELATED FIELDS In a slight bending of the rules, implicit LABEL s can contain more than one form control in our case, a radio INPUT as well as a SELECT ion box <fieldset class="radio related"> <legend><span>Confine results to</span></legend> <ul> <li> <label><input type="radio" name="confines" value="year" /> within the last year</label> </li> <li> < label > < input type = "radio" name = "confines" value = "month" /> the month of < select name = "month" > < option value = "01" > January </ option > ... < option value = "12" > December </ option > </ select > </ label > </li> ... </ul> </fieldset>
66. COMPLEX FORM: RELATED FIELDS How do I code that?!? think about it... what are the relationships of the fields? <fieldset class="radio related"> <legend><span>Confine results to</span></legend> <ul> ... <li> < dl > < dt > <!-- radio will go here --> </ dt > < dd > <!-- related fields here --> </ dd > </ dl > </li> </ul> </fieldset>
67. COMPLEX FORM: RELATED FIELDS We know the first bit <fieldset class="radio related"> <legend><span>Confine results to</span></legend> <ul> ... <li> <dl> <dt> < label > < input type = "radio" name = "confines" value = "range" /> a monthly range </ label > </dt> <dd> <!-- related fields here --> </dd> </dl> </li> </ul> </fieldset>
68. COMPLEX FORM: RELATED FIELDS We need to organize this now <fieldset class="radio related"> <legend><span>Confine results to</span></legend> <ul> ... <li> <dl> <dt> <label><input type="radio" name="confines"... </dt> <dd> < ol > < li > <!-- start --> </ li > < li > <!-- end --> </ li > </ ol > </dd> </dl> </li> </ul> </fieldset>
69. COMPLEX FORM: RELATED FIELDS Simple explicit LABEL ... <dd> <ol> <li> < label for = "range-start" > from the start of </ label > < select id = "range-start" name = "range-start" > < option value = "2006-01" > January 2006 </ option > ... < option value = "2006-12" > December 2006 </ option > </ select > </li> <li> <!-- end --> </li> ... </ol> </dd> ...
70. COMPLEX FORM: RELATED FIELDS And again ... <dd> <ol> <li> <label for="range-start">from the start of</label> ... </li> <li> < label for = "range-end" > until the end of </ label > < select id = "range-end" name = "range-end" > < option value = "2006-01" > January 2006 </ option > ... < option value = "2006-12" > December 2006 </ option > </ select > </li> </ol> </dd> ...
71. COMPLEX FORM: RELATED FIELDS <fieldset class="radio related"> <legend><span>Confine results to</span></legend> <ul> <li> <label> <input type="radio" name="confines" value="year" /> within the last year</label> </li> <li> <label> <input type="radio" name="confines" value="month" /> the month of <select name="month"> <option value="01">January</option> ... </select> </label> </li> <li> <dl> <dt> <label> <input type="radio" name="confines" value="range" /> a monthly range </label> </dt> <dd> <ol> <li> <label for="range-start">from the start of</label> <select id="range-start" name="range-start"> <option value="2006-01">January 2006</option> ... </select> </li> <li> <label for="range-end">until the end of</label> <select id="range-end" name="range-end"> <option value="2006-01">January 2006</option> ... </select> </li> </ol> </dd> </dl> </li> </ul> </fieldset> Itʼs a lot of code...
72. COMPLEX FORM: RELATED FIELDS ...but the benefits are worth it Confine results to List of three items bullet Radio button (not checked) within the last year bullet Radio button (not checked) the month of Combo box January bullet Definition list of one item Radio button (not checked) a monthly range equals List of two items one: from the start of Combo box January 2006 two: until the end of Combo box January 2006 List end List end List end transcribed by Fangs
73. COMPLEX FORM: RELATED FIELDS <fieldset class="radio related"> <legend><span>Confine results to</span></legend> <ul> <li> <!-- year --> </li> <li> <!-- month --> </li> <li> <!-- range --> </li> </ul> </fieldset> /* We'll save some space and inherit styles from .radio */ form ol, form ul, form dl { list-style: none; margin: 0; padding: 0; } li ul, li ol { font-size: 1em; }
74. COMPLEX FORM: RELATED FIELDS <fieldset class="radio related"> <legend><span>Confine results to</span></legend> <ul> <li> <!-- year --> </li> <li> <!-- month --> </li> <li> <!-- range --> </li> </ul> </fieldset> .related li { clear: both; float: none; margin: 0 0 .5em; width: auto; } /* For IE to recover from a strange margin */ .related li { zoom: 1; }
79. MESSAGING: REQUIRED What is the * anyway? Well, it stands for something else and in HTML, the closest to that we have to convey that is the ABBR element. <form id="contact-form" action="test.php" method="get"> <fieldset> <legend>Send us a message</legend> <p> Required fields are marked <abbr title = "required" > * </abbr> . </p> <ol> <li> <label for="contact-name"> Name < abbr title = "required" > * </ abbr > </label> <input type="text" id="contact-name" name="name" /> </li> ...
80. MESSAGING: REQUIRED If you want to go all-out, you can but that seems like overkill <form id="contact-form" action="test.php" method="get"> <fieldset> <legend>Send us a message</legend> <p> Required fields are marked <em><abbr title = "required" > * </abbr></em>. </p> <ol> <li class = "required" > <label for="contact-name"> Name <em>< abbr title = "required" > * </ abbr ></em> </label> <input type="text" id="contact-name" name="name" /> </li> ...
90. MESSAGING: ERRORS How should we strongly emphasize even more important error advisories? How should we highlight the field? ... <li class = "error" > <label for="contact-email"> Email<abbr title="required">*</abbr> < strong class = "err" > You forgot to fill in your email </ strong > </label> <input type="text" id="contact-email" name="email" /> </li> ...
91. MESSAGING: ERRORS ... <li class="error"> <label for="contact-email"> Email<abbr title="required">*</abbr><strong class="err"> You forgot to fill in your email</strong> </label> <input type="text" id="contact-email" name="email" /> </li> ...
92. MESSAGING: ERRORS ... <li class="error"> <label for="contact-email"> Email<abbr title="required">*</abbr><strong class="err"> You forgot to fill in your email</strong> </label> <input type="text" id="contact-email" name="email" /> </li> ... strong.err { color: #ffdfdf; display: block; padding-left: 5px; text-align: left; }
94. MESSAGING: ERRORS ... <li class="error"> <label for="contact-email"> Email<abbr title="required">*</abbr><strong class="err"> You forgot to fill in your email</strong> </label> <input type="text" id="contact-email" name="email" /> </li> ... .error input { border: 2px solid #b00; background: #ffdfdf; } .error input:focus { background: #fff; }
95. MESSAGING: ERRORS How do we notify users of errors? It is best to be upfront about errors encountered and to say, in plain language, what the user needs to do to fix it and linking to the field with errors doesn't hurt <form id="contact-form" action="test.php" method="get"> <fieldset> <legend>Send us a message</legend> < div id = "errors" > < p > We encountered < strong > 1 error </ strong > while trying to process this form: </ p > < ol > < li > You forgot to include < a href = "#contact-email" > your email address </ a ></ li > </ ol > </ div > <p>Required fields are marked <abbr title="required">*</abbr>.</p> ...
96. MESSAGING: ERRORS <form id="contact-form" action="test.php" method="get"> <fieldset> <legend>Send us a message</legend> <div id="errors"> <p>We encountered <strong>1 error</strong> while trying to process this form:</p> <ol> <li>You forgot to include <a href="#contact-email">your email address</a></li> </ol> </div> <p>Required fields are marked <abbr title="required">*</abbr>.</p> ...
97. MESSAGING: ERRORS <form id="contact-form" action="test.php" method="get"> <fieldset> <legend>Send us a message</legend> <div id="errors"> <p>We encountered <strong>1 error</strong> while trying to process this form:</p> <ol> <li>You forgot to include <a href="#contact-email">your email address</a></li> </ol> </div> <p>Required fields are marked <abbr title="required">*</abbr>.</p> ... #errors { background: #ffdfdf; border: 2px solid #b00; color: #333; margin: .5em 0 1em; padding: 10px; }
98. MESSAGING: ERRORS <form id="contact-form" action="test.php" method="get"> <fieldset> <legend>Send us a message</legend> <div id="errors"> <p>We encountered <strong>1 error</strong> while trying to process this form:</p> <ol> <li>You forgot to include <a href="#contact-email">your email address</a></li> </ol> </div> <p>Required fields are marked <abbr title="required">*</abbr>.</p> ... #errors p { margin: 0; padding: 0; } #errors ol { list-style: disc; margin: 0 0 0 20px; }
99. MESSAGING: ERRORS <form id="contact-form" action="test.php" method="get"> <fieldset> <legend>Send us a message</legend> <div id="errors"> <p>We encountered <strong>1 error</strong> while trying to process this form:</p> <ol> <li>You forgot to include <a href="#contact-email">your email address</a></li> </ol> </div> <p>Required fields are marked <abbr title="required">*</abbr>.</p> ... #errors a { color: #b00; }
100. PARTING ADVICE • treat forms like any other piece of (X)HTML • be aware of browser inconsistencies & make accommodations (when possible) • break a complex form into bite-size chunks • look for ways to provide richer meaning/a better experience • apply your CSS layout knowledge for the rest of the page to a form • don't over-complicate form design • learn to love forms