<form> Creating XHTML FormsYou use the <form> element to create an XHTML form, which you use to contain XHTML controls such as buttons and text fields. This element is supported in XHTML 1.0 Strict, XHTML 1.0 Transitional, XHTML 1.0 Frameset, and XHTML 1.1. Here are its attributes:
Here are the XHTML events this element supports: onclick , ondblclick , onmousedown , onmouseup , onmouseover , onmousemove , onmouseout , onkeypress , onkeydown , onkeyup , onsubmit , and onreset . When you want to use controls, such as buttons and text fields, in a Web page, you should enclose the control elements in a <form> element. (Controls won't even appear in Netscape Navigator unless you enclose them in a <form> element.) Forms were originally intended to be used to send data (such as the contents of text fields) back to the server. The target URI to send that data to is placed in the action attribute, and the W3C has made that attribute required for forms. We'll see how to send data to a server in Chapter 20, "WML, ASP, JSP, Servlets, and Perl," and we'll use the action attribute there; until then, I'll just set the action attribute to "action" . In addition, note that in XHTML, you're supposed to give XHTML forms an ID with the ID attribute. However, some browsers, such as Netscape Navigator, don't understand that attribute and use name instead. The only alternative in this case is to use both the ID and the name attributes here. Let's see an example. Here, the document will display a message that, when clicked, displays Hello from JavaScript . in a text field. Here's the code (note that the <br> element will be deprecated in XHTML 2.0): Listing ch17_15.html<?xml version="1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/tr/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title> Using Forms in XHTML </title> <script language = "javascript"> function displayMessage() { document.form1.textfield.value = "Hello from JavaScript." } </script> </head> <body> <center> <h1> Using Forms in XHTML </h1> <form name = "form1" id = "form1" action = "action"> <input type = "text" name = "textfield" size = "25" /> <br /> <br /> <input type = "button" value = "Click Me" onclick = "displayMessage()" /> </form> </center> </body> </html> You can see the results of this XHTML in Figure 17-7. Figure 17-7. Using an XHTML form in the Netscape Navigator.
This document used both a button and a text field. I'll take a look at how to create those two controls in XHTML next . |