More JavaScript. JavaScript and HTML forms One of the most common usages of JavaScript is to respond to user actions when completing HTML forms JavaScript.

Slides:



Advertisements
Similar presentations
LIS651 lecture 0 forms Thomas Krichel
Advertisements

23-Aug-14 HTML/XHTML Forms. 2 What are forms? is just another kind of XHTML/HTML tag Forms are used to create (rather primitive) GUIs on Web pages Usually.
24-Aug-14 HTML Forms. 2 What are forms? is just another kind of HTML tag HTML forms are used to create (rather primitive) GUIs on Web pages Usually the.
JavaScript Forms Form Validation Cookies. What JavaScript can do  Control document appearance and content  Control the browser  Interact with user.
Chapter 6 Basic forms 1. Forms and query string 2 form : a group of user input (UI) controls that accepts information from the user and sends the information.
SE-2840 Dr. Mark L. Hornick 1 HTML input elements and forms.
Supplement Creating Forms. Objectives Show how forms are used How to create the Form element HTML elements used for creating input fields.
The HTMLSelectElement class. Attributes and methods of the HTMLSelectElement class Attributes readonly DOMString type ; long selectedIndex ; DOMString.
COS 125 DAY 25. Agenda Assignment #7 Graded  2 A’s, 3 B’s, 2 C’s, 1 D, 2 F’s (late) and 5 non-submit Assignment # 8 Due Today Assignment #9 is posted.
Capturing user input: Using HTML FORMs User input Up till now, our HTML documents have all been directed at outputting information to the user However,
COS 125 DAY 23. Agenda Assignment 6 Corrected  5 A’s, 1 B, 1 C and 1 D  Problems with using CSS for layouts Assignment 7 is due today Assignment 7 Assignment.
The SELECT element This type of element merely offers another way of capturing the kinds of user-selection that we have already seen how to capture with.
Creating Web Page Forms. Objectives Describe how Web forms can interact with a server-based program Insert a form into a Web page Create and format a.
Tutorial 6 Working with Web Forms
Capturing user input: Using HTML FORMs CS4320 got here on 27/11/2003.
Form Basics CS Web Data  Most interesting web pages revolve around data  examples: Google, IMDB, Digg, Facebook, YouTube, Rotten Tomatoes  can.
The conditional statement General form: if ( ) { consequent-action-list } Actual Example: if (document.getElemendById(‘name’).value= ='') { alert(‘Name.
Other types of input element There are some types of input element that we have not considered: –type='image’ –type='hidden’ We will delay dealing with.
Tutorial 6 Working with Web Forms. XP Objectives Explore how Web forms interact with Web servers Create form elements Create field sets and legends Create.
Creating Web Page Forms
Web Development & Design Foundations with XHTML Chapter 9 Key Concepts.
Forms, Validation Week 7 INFM 603. Announcements Try placing today’s example in htdocs (XAMPP). This will allow you to execute examples that rely on PHP.
CSE 154 LECTURE 9: FORMS. Web data most interesting web pages revolve around data examples: Google, IMDB, Digg, Facebook, YouTube, Rotten Tomatoes can.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
Lesson 8 Creating Forms with JavaScript
CST JavaScript Validating Form Data with JavaScript.
1 Web Developer & Design Foundations with XHTML Chapter 6 Key Concepts.
INTRODUCTION TO WEB DEVELOPMENT AND HTML Lecture 07: Forms - Spring 2011.
4-Sep-15 HTML Forms Mrs. Goins Web Design Class. Parts of a Web Form A Form is an area that can contain Form Control/Elements. Each piece of information.
XP Tutorial 6New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Creating Web Page Forms Designing a Product Registration Form Tutorial.
Forms and Form Controls Chapter What is a Form?
JQuery and Forms – Part I. Learning Objectives By the end of this lecture, you should be able to: – Retrieve the values entered by a user into a form.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Chapter 6: Forms JavaScript - Introductory. Previewing the Product Registration Form.
XHTML Introductory1 Forms Chapter 7. XHTML Introductory2 Objectives In this chapter, you will: Study elements Learn about input fields Use the element.
225 City Avenue, Suite 106 Bala Cynwyd, PA , phone , fax presents… HTML Lists, Tables and Forms v2.0.
JavaScript Lecture 6 Rachel A Ober
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
Suzanne J. Sultan Javascript Lecture 2. Suzanne J. Sultan function What is a function> Types of functions:  Function with no parameters  Function with.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
 Whether using paper forms or forms on the web, forms are used for gathering information. User enter information into designated areas, or fields. Forms.
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) HTML Forms James Wang.
HTML Forms.
LOGO FORMs in HTML CHAPTER 5 Eastern Mediterranean University School of Computing and Technology Department of Information Technology ITEC229 Client-Side.
Web Development & Design Foundations with XHTML Chapter 9 Key Concepts.
JavaScript - A Web Script Language Fred Durao
Tutorial 6 Working with Web Forms. XP Objectives Explore how Web forms interact with Web servers Create form elements Create field sets and legends Create.
Tutorial 6 Working with Web Forms. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Explore how Web forms interact with.
Creating Web Page Forms. Introducing Web Forms Web forms collect information from users Web forms include different control elements including: –Input.
IS2802 Introduction to Multimedia Applications for Business Lecture 5: JavaScript, form validation and browser detection Rob Gleasure
HTML Forms. Slide 2 Forms (Introduction) The purpose of input forms Organizing forms with a and Using different element types to get user input A brief.
HTML Forms a form is a container for input elements on a web page input elements contain data that is sent to the web server for processing.
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
Tutorial 6 Working with Web Forms. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Explore how Web forms interact with.
HTML FORMS The TEXT Object Presented By: Ankit Gupta.
HTML Forms.
COS 125 DAY 17. Agenda  Assignment 7 not finished grading  Assignment 8 posted Due April 9  Capstone progress reports due  Quiz 2 Corrected 2 A’s,
Check Boxes. 2 Check boxes  Image from:  HTML:  Each box gets it's own.
Introduction to JavaScript Events Instructor: Sergey Goldman 1.
HTML III (Forms) Robin Burke ECT 270. Outline Where we are in this class Web applications HTML Forms Break Forms lab.
Introduction to Web Site Development Department of Computer Science California State University, Los Angeles Lecture 8: Forms and Controls.
FORMS Explained By: Jasdeep Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
XP Tutorial 6New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Creating Web Page Forms Designing a Product Registration Form Tutorial 6.
Client-side (JavaScript) Validation. Associating a function with a click event – Part 1 Use the input tag’s onclick attribute to associate a function.
Chapter 5 Validating Form Data with JavaScript
CS3220 Web and Internet Programming HTML Tables and Forms
Conditions and Ifs BIS1523 – Lecture 8.
JavaScript and Forms Kevin Harville.
Presentation transcript:

More JavaScript

JavaScript and HTML forms One of the most common usages of JavaScript is to respond to user actions when completing HTML forms JavaScript enables us to process HTML forms by providing –a large library of object classes with associated methods and attributes This library is part of a much bigger library, the DOM (Document Object Model) library that will be introduced in CS 4408 Now, however, we will consider just a small part of it, connected with HTML form elements

Processing input elements We have seen that we can access any HTML element on a page if it has an id attribute Remember the form which contained this: What is your name? We could check if its value is the empty string with this JavaScript statement if ( document.getElementById( ‘ name ’ ).value =='') { alert("Name is empty"); }

The JavaScript object class for representing input elements We have already seen that the this object class contains an attribute called value We have just read the value of this attribute But we can also change its value Consider the form on the next slide –which asks for the colour of the user’s hair and eyes

Initial state of form

The user gives his hair colour as ‘brown’ and hits the tab key on his keyboard

The cursor flashes in the eye-colour input box, which is still empty, waiting for him to type something

The user can now declare his eye colour

But, let’s refresh the form

Suppose the user declares his hair colour is ‘red’. Before he hits the tab key, the cursor is still in the hair-colour box and the eye-colour box is empty

But, immediately after he hits the tab key, the value ‘blue’ appears in the eye-colour box

Although, if he wants, the user can replace the eye-colour ‘blue’ with some other colour

Specification for the form we have just seen form { background-color:yellow; width:300; padding:0.1in } function respond() { var hairColour=document.getElementById('hairColour'); var eyeColour=document.getElementById('eyeColour'); if (hairColour.value=='red') { eyeColour.value='blue'; } } Colour of your hair? Colour of your eyes? Submit details

Attributes and methods of the HTMLInputElement class Attributes DOMString defaultValue; boolean defaultChecked; readonly HTMLFormElement form; DOMString accept; DOMString accessKey; DOMString align; DOMString alt; boolean checked; boolean disabled; long maxLength; DOMString name; boolean readOnly; DOMString size; DOMString src; long tabIndex; readonly DOMString type; DOMString useMap; DOMString value; Methods void blur() ; void focus() ; void select() ; void click() ;

Notice that some attributes are readonly Attributes DOMString defaultValue; boolean defaultChecked; readonly HTMLFormElement form; DOMString accept; DOMString accessKey; DOMString align; /*deprecated from HTML 4.0 onwards*/ DOMString alt; boolean checked; boolean disabled; long maxLength; DOMString name; boolean readOnly; DOMString size; DOMString src; long tabIndex; readonly DOMString type; DOMString useMap; DOMString value; The form attribute of a HTMLInputElement object lets us access the object which represents the entire form of which the input element forms a part The type attribute of a HTMLInputElement object gives us the type of the input element We cannot change either of these

Using the size attribute Remember that Spanish names are often very long We might want to cater for that on a form which asks where the user comes from and what his name is

Initial state of the form

Suppose the user has stated he is from ‘Ireland’ but has not yet hit the tab key

After he has hit the tab key, the size of the input element for entering his name is unchanged

And the user just enters his name as usual

But, let’s refresh the form

Suppose the user declares she is from Spain, but has not yet hit the tab key

After she has hit the tab key, the box in which she can enter her name gets much bigger

So she can type in a long Spanish name and still see it all

Specification of previous form form { background-color:yellow; width:600; padding:0.1in } function respond() {var country=document.getElementById('country'); var name=document.getElementById('name'); if (country.value=='Spain') { name.size=80; } } What country are you from? What is your name? Submit details

Sometimes, people from other countries have long name We could let the user declare she has a long name, by clicking on a checkbox

When the user has declared her country and hit the tab key, focus moves to the checkbox

The user has clicked the checkbox but has not yet hit the tab key

The user has just hit the tab key and the name box has become bigger

So she can now type in her long name and still see it all

Specification of previous form form { background-color:yellow; width:600; padding:0.1in } function respond() {var longName=document.getElementById('longName'); var name=document.getElementById('name'); if (longName.checked) { name.size=80; } } What country are you from? Your name? Long name? <input type="checkbox" name="longName" id="longName“ onchange="respond()" > Submit details

On this form, the company is trying to push sales of Superman’s Cloak so it is pre-checked on the form

Suppose the user wants only Batman’s Cloak, …

Suppose the user wants only Batman’s Cloak, so he clicks on the first checkbox, and then …

… he unchecks the box for Superman’s Cloak, and then …

and then when he clicks on the box for entering his name, an alert box pops us

Specification of previous form form { background-color:yellow; width:600; padding:0.1in } li { list-style-type : none } function respond(someString) { var clickedItem=document.getElementById(someString); if (clickedItem.defaultChecked && !clickedItem.checked) { alert("Are you really sure you do not want Superman's cloak?"); } } Your T-shirt selection? Batman's cloak <input type="checkbox" name="products" id="products1" value='batman' onchange="respond('products1')" > Superman's cloak <input type="checkbox" name="products" id="products2" value='superman' onchange="respond('products2')“ checked='checked‘ >(our most popular item) Dr. Who's coat <input type="checkbox" name="products" id="products3" value='drWho' onchange="respond('products3')" > What is your name? Submit order

We can generalize the previous approach form { background-color:yellow; width:600; padding:0.1in } li { list-style-type : none } function respond(someString) {var clickedItem=document.getElementById(someString); if (clickedItem.defaultChecked && !clickedItem.checked) { alert("Are you really sure you do not want "+clickedItem.value+"?"); } } Your T-shirt selection? Batman's cloak Superman's cloak (our most popular item) Dr. Who's coat (another very popular item) What is your name? Submit order

A reminder: checking forms again

Suppose the user clicks on the ‘Check form’ button

He is warned that he has not declared his country and …

… that he has not declared his name

A better way to do this

It would be better if we did not have a special button for checking that the form is completed correctly It would be better if the form-checking task could be performed by the button that we use to submit the data on the form This button should –Check whether the form is completed correctly If it is, the data should be submitted If it is not, a warning message should be produced

Form has just one button

User clicks on this button and gets a warning

User specifies country and clicks on button … Now get a different warning

User specifies both country and name and …

User specifies both country and name and clicks on the button and …

User specifies both country and name and clicks on the button and … the data are sent to the server, which replies

Text of previous PHP program form { background-color:yellow; width:600; padding:0.1in } function maybeSubmit() {if ( !errorFoundIn('country') && !errorFoundIn('name') ) { var form1=document.getElementById('form1'); form1.submit(); } } function errorFoundIn(someString) {var someItem=document.getElementById(someString); if (someItem.value=='') { alert('You have not specified your '+someString); return true; } else { return false; } } <?php $country=$_POST['country']; $name=$_POST['name']; if ($country && $name) { echo " Hello $name from $country "; } else {?> " id="form1“ > What is your country? What is your name? Submit details <?php } ?>

We have just used the submit() method from the HTMLFormElement object class Attributes readonly HTMLCollection elements; readonly long length; DOMString name; DOMString acceptCharset; DOMString action; DOMString enctype; DOMString method; DOMString target; Methods void submit() ; void reset() ;

Now, that we know about the submit() method We can use it instead of having to use the input of type=‘image’ If we want to use a picture as a submit button, we can just –Put the picture on the form using an img element –Give the img element an onclick attribute Which uses the submit() method of the form object

Using an image with the submit() method form { width : 400; background-color:gray; padding:0.1in legend { color : white }.myImage { height:25; width:25 } function mySubmit() {var form1=document.getElementById('form1'); form1.submit(); } <?php $surname=$_POST['surname']; if ($surname) {echo "Your surname is $surname";} else {?> " id="form1“ > What is your surname? Submit data <?php } ?> Example program

Fresh form

User enters data and …

User enters data and clicks on image and …

User enters data and clicks on image and the server replies

Now, back to the HTMLInputElement object class

In the HTMLInputElement class, the attribute defaultValue can be used to access any value which is specified on the original form Attributes DOMString defaultValue; boolean defaultChecked; readonly HTMLFormElement form; DOMString accept; DOMString accessKey; DOMString align; DOMString alt; boolean checked; boolean disabled; long maxLength; DOMString name; boolean readOnly; DOMString size; DOMString src; long tabIndex; readonly DOMString type; DOMString useMap; DOMString value; Methods void blur() ; void focus() ; void select() ; void click() ;

This form has no fieldset legends

The user is told what must be entered because the input elements contain some default text " id="form1"> Submit details

But, now, when we are checking if the form has been completed correctly, we cannot just check for the empty string –because the inputs always contain a non- empty string Instead, a correctly completed form does not have the default value in any of its input elements

Text of appropriate PHP program form { background-color:yellow; width:600; padding:0.1in } function maybeSubmit() {if ( !errorFoundIn('country') && !errorFoundIn('name') ) { var form1=document.getElementById('form1'); form1.submit(); } } function errorFoundIn(someString) {var someItem=document.getElementById(someString); if ( someItem.value==someItem.defaultValue ) { alert('You have not specified your '+someString); return true; } else { return false; } } <?php $country=$_POST['country']; $name=$_POST['name']; if ($country && $name) { echo " Hello $name from $country "; } else {?> " id="form1"> Submit details <?php } ?>

Fresh form

Error alert is produced when user clicks button on an incomplete form

When user clicks button on a complete form, …

When user clicks button on a complete form, the data are sent to the server, which replies

For more information about the HTMLInputElement object class Go to this web-page and search for the text “Interface HTMLInputElement”