Forms and Scripting

 <  Day Day Up  >  


As presented here, forms really don't finish the job. It is easy to create a form that asks users for their name and the number of gadgets they want to order. It also is easy to write a server-side program ( assuming you're a programmer) that can take submitted form data and do something with it. However, it is not easy to make sure that the submitted data is correct. Why should you let the user enter a quantity of “10 (negative ten) gadgets in the form and submit it when that is obviously wrong? Sure, the server-side program could catch this, but it's best to try to catch this at the browser level before submitting the form for processing. This is one of the main reasons for client-side scripting. Of course, you'll probably end up having to do both client- as well as server-side validation just to be on the safe side.

Starting with Netscape 2.0 and continuing until today, it has been possible to use a scripting language such as JavaScript to associate scripts with user-generated events in a browser. To handle events for a form control, set an event handler using an attribute that corresponds to the name of the event. If you want to trigger a script when a button is pressed, you could insert some script code associated with the event attribute, as shown in the following dummy form:

  <form>   <input type="button" value="Don't Press Me!"   onclick="alert('Danger! Danger!');" />   </form>  

Events are added to form controls using attribute declarations such as onclick , onsubmit , onreset , and so on. The number of events has grown significantly and now applies to elements outside forms. In Chapter 14, you will find a complete example of checking form fields, called form validation, using JavaScript. Before wrapping up the chapter, let's briefly discuss an emerging technology that may completely change the way that forms are handled in Web sites ”XForms.



 <  Day Day Up  >  


HTML & XHTML
HTML & XHTML: The Complete Reference (Osborne Complete Reference Series)
ISBN: 007222942X
EAN: 2147483647
Year: 2003
Pages: 252
Authors: Thomas Powell

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net