Programming the Web using XHTML and JavaScript

Slides:



Advertisements
Similar presentations
JavaScript and the DOM Les Carr COMP3001 Les Carr COMP3001.
Advertisements

The Web Warrior Guide to Web Design Technologies
Tutorial 6 Working with Web Forms
Forms Review. 2 Using Forms tag  Contains the form elements on a web page  Container tag tag  Configures a variety of form elements including text.
Web-based Application Development Lecture 17 March 16, 2006 Anita Raja.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
Web-based Application Development Lecture 18 March 21, 2006 Anita Raja.
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.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Introduction to JavaScript for Python Programmers
Tutorial 6 Forms Section A - Working with Forms in JavaScript.
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.
CST JavaScript Validating Form Data with JavaScript.
1 Web Developer & Design Foundations with XHTML Chapter 6 Key Concepts.
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.
Lecture 6 – Form processing (Part 1) SFDV3011 – Advanced Web Development 1.
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.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
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.
Using Client-Side Scripts to Enhance Web Applications 1.
JavaScript, Fourth Edition Chapter 5 Validating Form Data with JavaScript.
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
Flow of Control Part 1: Selection
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.
Microsoft FrontPage 2003 Illustrated Complete Creating a Form.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
HTML Form Widgets. Review: HTML Forms HTML forms are used to create web pages that accept user input Forms allow the user to communicate information back.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
JavaScript, Fourth Edition
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.
Controlling Program Flow with Decision Structures.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Lesson 5 Introduction to HTML Forms. Lesson 5 Forms A form is an area that can contain form elements. Form elements are elements that allow the user to.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Tutorial 6 Working with Web Forms
JavaScript, Sixth Edition
Chapter 5 Validating Form Data with JavaScript
CHAPTER 10 JAVA SCRIPT.
In this session, you will learn to:
Chapter 4 The If…Then Statement
ITM 352 HTML Forms, Basic Form Processing
Making Choices with if Statements
Forms Web Design Ms. Olifer.
Programming the Web using XHTML and JavaScript
Chapter 6: Conditional Statements and Loops
Web Programming– UFCFB Lecture 17
Introducing Forms.
More Selections BIS1523 – Lecture 9.
Introduction to JavaScript for Python Programmers
FORM OBJECT When creating an interactive web site for the Internet it is necessary to capture user input and process this input. Based on the result of.
T. Jumana Abu Shmais – AOU - Riyadh
Chapter 5: Control Structure
Web Development Using ASP .NET
JavaScript and Forms Kevin Harville.
Introducing JavaScript
Selection Statements Chapter 3.
CIS 136 Building Mobile Apps
Controlling Program Flow
Web Programming and Design
Presentation transcript:

Programming the Web using XHTML and JavaScript Chapter 12 Increasing the Interactivity

Conditionals!

Conditional Statements So far… Input some data Output some data Used variables to store information Modified information & page characteristics Basically straight line processing Now, address the need to respond to user’s input and make choices

Conditional Statements Poses a question that is Unambiguously true or false then Executes one set of statements if true and Optionally executes a different set if false

Conditional Statements Basic syntax (pseudocode): if some condition is true execute these statements otherwise execute these statements Optional

Conditional Statements JavaScript syntax: if (…) { … } Defines conditional statement Keyword Conditional statement Defines block Statement(s) to be executed if condition is true

Conditional Statements The question involves a comparison defined by a relational operator var result = 12 … if ( result == 12 ) { } Assignment operator – an action Equality operator – a test

Conditional Statements As code in the body Ch12-Ex-01 In a function Ch12-Ex-01a

Conditional Statements Relational Operators Symbol Meaning < Less than > Greater than <= Less than or equal to >= Greater than or equal to != Not equal to == Equal to

Conditional Statements Condition syntax: operand operator operand variable operator variable result1 <= result2 variable operator constant result1 != 12

Conditional Statements Program flow statement x statement y if (condition is true) statement a statement z

Conditional Statements Program flow statement x statement y if (condition is false) statement a statement z

Conditional Statements One way or another, statement z is being executed statement x statement y if (condition is false) statement a statement z if statement

Conditional Statements Ch12-Ex-02

Conditional Statements JavaScript syntax (optional ways to code): if (…) statement or if (…) { statement(s) } if (…) { statement(s) } Note: this is one single statement Note: these can be one or more statements The curly braces {} are “optional” if you only want to execute one statement for the if, but you must be careful!

Conditional Statements JavaScript syntax (optional else): if (…) { … } else { … } Ch12-Ex-03 if clause else clause

Conditional Statements Compound conditionals if (… ) { … } else if (…){ … } else if (…){ … } else { … } Ch12-Ex-04 Repeat as many times as needed Final else (optional – use if needed)

Conditional Statements Nested conditionals if (firstNum > 12) { if (firstNum < 25) { alert(“Number is between 12 and 25”) } statement x Nested conditionals if (firstNum > 12) { if (firstNum < 25) { alert(“Number is between 12 and 25”) } statement x

Conditional Statements Nested conditionals Error in this code: won’t always alert the right message var Num = GetNum() // confirm if number between 12 to 25 // warn otherwise if (Num > 12) { if (Num < 25) { alert(“Number is between 12 and 25”) } else { alert(“The number is out of bounds”)

Conditional Statements Nested conditionals if (Num > 12) { if (Num < 25) { alert(“Number is between 12 and 25”) } else { alert(“The number is out of bounds”)

Conditional Statements Nested conditionals – fix? if (Num > 12) { if (Num < 25) { alert(“Number is between 12 and 25”) } else { alert(“The number is out of bounds”) statement x

Conditional Statements Logical operators “and” - && “or” - || “not” - !

Conditional Statements Logical operators if ( (Num > 12) && (Num < 25) ) { alert(“Number is between 12 and 25”) } else { alert(“The number is out of bounds”) statement x

Conditional Statements Truth tables Prop. 1 (type) T F Prop. 2

Conditional Statements “Today is Tuesday and we’re in class” Today is Tue. AND T F We’re in class T F F F

Conditional Statements “Today is Friday or we’re in class” Today is Friday OR T F We’re in class T T T F

Conditional Statements “Today is Friday” Today is Friday T F NOT F T

Conditional Statements True cases var found found = searchDatabase(data) if (found) alert(“Found it”) else alert(“Item not found”) What is the bad programming practice above?

Conditional Statements True cases var found found = searchDatabase(data) if (found==true) alert(“Found it”) else alert(“Item not found”)

Conditional Statements False cases using “not” var found found = searchDatabase(data) if (! found) alert(“Item not found”) else alert(“Found it”)

Conditional Statements Using “not” var found found = searchDatabase(data) if (found == false) alert(“Item not found”) else alert(“Found it”)

Conditional Statements Using “not” var found found = searchDatabase(data) if (found != true) alert(“Item not found”) else alert(“Found it”)

Back to Forms!

Check boxes

Check Boxes Captures user responses To multiple Yes/No or True/False situations Basic syntax: <input type = “checkbox” name = “perlCB” checked = “checked” /> Can have as many checkboxes as you need Can check as many as you like Each checkbox has a unique name Note: <input type=‘checkbox’ …> makes a check box and nothing else i.e. no text goes with it You need to supply text or graphics to give the box meaning

Check Boxes Each check box is an object Each has a checked property Value can be true or false document.formName.checkboxName.checked Ch12-Ex-05 Enhanced?: Ch12-Ex-05a Note the subtle logic error!

Check Boxes Check boxes include an onclick event Ch12-Ex-06

Radio Buttons

Radio Buttons Captures user response <input type = “radio” Use in multiple choice, mutually exclusive situations For every button within that group or set Basic syntax: <input type = “radio” name = “sodaRB” /> Can have multiple sets of radio buttons Each button in a set must have the same name to be grouped together Can check one and only one within the group having the same name As with the checkbox this makes the radio button only No names, titles, etc. associated with button Must add your own

Radio Buttons Example: <form …> <input type=“radio” name=“sodaRB” />Coke<br/> <input type=“radio” name=“sodaRB” />Pepsi<br/> <input type=“radio” name=“sodaRB” />Sprite<br/> <input type=“radio” name=“sodaRB” />Beer<br/> </form>

Radio Buttons Every one has the same name! Use array element notation: How to test which was selected? Use array element notation: document.formName.sodaRB[0] document.formName.sodaRB[1] … document.formName.sodaRB[n] Ch12-Ex-07

AKA drop downs Pop ups

Pop-Up Menus Selections appear when the user selects the menu Doesn’t take up space until needed Makes them somewhat better than radio buttons when used for a similar purpose Especially for lots of items to be listed

Defines the individual menu choices Pop-Up Menus Created with a form Uses select and option elements: <form …> <select name=“…”> <option> … </option> </select> </form> Ch12-Ex-08 Defines the individual menu choices Creates the menu

Pop-Up Menus Menu objects have a selectedIndex property The first menu item’s index is zero The second menu item’s index is one, etc. The full name of the property is document.formName.selectName.selectedIndex Use an if statement to find out which menu item was selected

Pop-Up Menus To make the menu work nicely we’ll add: Ch12-Ex-09 Some explanatory text A button to invoke a function A function to do process the menu when the button is pressed Ch12-Ex-09

Pop-Up Menus The <option> element includes a value attribute: <option value=“I like Coke”>Coke</option> Referred to by: document.formName.selectName.options[n].value Ch12-Ex-10

Pop-Up Menus Sometimes you might not want to give one item preference So include a dummy item to start Ch12-Ex-11 Should the top element be “selectable”? Ch12-Ex-11a

Pop-Up Menus On the other hand perhaps you’d like a default choice Add selected=“selected” to option Ch12-Ex-12

Pop-Up Menus Quick links menus can be created using: Ch12-Ex-13 The value attribute to hold URLs for each option The onchange event handler for the select element Ch12-Ex-13 What is an odd behavior in Ex 13?

More on Forms … Form attributes: Method: tells how to pass form elements on Use method=“post” to hide details Enctype: how to send the message Use enctype=“text/plain” to send the message as unencrypted text Action: what to do when the form is submitted What program is executed when submitted Use mailto to do an email: action=“mailto:userid@uncc.edu” Include name attributes for all form elements The general form for information submitted via a form is: Name_Of_Form_Element = Value_Of_Information_Entered Ch12-Ex-14

More on Forms …

Assignment See Assignments Web Page