Retrieving information from forms

Slides:



Advertisements
Similar presentations
Modifying existing content Adding/Removing content on a page using jQuery.
Advertisements

FORMs Lesson TBE 540. Prerequisites Before beginning this lesson, the learner must be able to… –Create basic web pages using a text editor and/or a web.
Escaping Characters document.write(). Learning Objectives By the end of this lecture, you should be able to: – Recognize some of the characters that can.
Creating Web Page Forms
Introduction to scripting
1 Events Lect 8. 2 Event-driven Pages one popular feature of the Web is its interactive nature e.g., you click on buttons to make windows appear e.g.,
MS3304: Week 4 PHP & HTML Forms. Overview HTML Forms elements refresher Sending data to a script via an HTML form –The post vs. get methods –Name value.
PHP : Hypertext Preprocessor
CSE 382/ETE 334 Internet and Web Technology Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU.
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.
Adding JavaScript (<script tag>)
Server vs Client-side validation. JavaScript JavaScript is an object-based language. JavaScript is based on manipulating objects by modifying an object’s.
Selectors. Learning Objectives By the end of this lecture, you should be able to: – Select items using jQuery based on ID, class name, or HTML tag. –
Robinson_CIS_285_2005 HTML FORMS CIS 285 Winter_2005 Instructor: Mary Robinson.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Working with Files. Learning Objectives By the end of this lecture, you should be able to: – Examine a file that contains code with unfamiliar material,
PHP Form Introduction Getting User Information Text Input.
Button and Textbox. Input  Input objects are used to obtain input from the user viewing the webpage. They allow the user to interact with the Web. 
1 HTML Forms
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.
Overview of Form and Javascript fundamentals. Brief matching exercise 1. This is the software that allows a user to access and view HTML documents 2.
Event-Driven Programming Chapter Page Section: Section or division of an HTML page Generic block element Example: What's the difference between.
Lesson 16. Practical Application 1 We can take advantage of JavaScript and the DOM, to set up a form so that the first text box of a form automatically.
Event Handling (the right way). A Simple Web Page Events - Summary The web page looks like this:
Changing HTML Attributes each() function Anonymous Functions $(this) keyword.
Review of HTML and CSS A (very) brief review of some key fundamentals to be aware of in IT-238.
1 HTML Forms
Multiple Items in one Data Object Arrays are a way to store more than one piece of data in a data object, provided that all the data is of the same type.
1 HTML forms (cont.)
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.
CSD 340 (Blum)1 Introducing Text Input elements and Ifs.
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
Ashima Wadhwa Java Script And Forms. Introduction Forms: –One of the most common Web page elements used with JavaScript –Typical forms you may encounter.
Client-side (JavaScript) Validation. Associating a function with a click event – Part 1 Use the input tag’s onclick attribute to associate a function.
JavaScript Part 1 Introduction to scripting The ‘alert’ function.
JavaScript: Conditionals contd.
Intro to Dynamic HTML Forms - Part 1
JavaScript Controlling the flow of your programs with ‘if’ statements
Precedence Operators Error Types
Moving away from alert() Using innerHTML Using an empty div section
HTML Elements with JavaScript and DOM Events
Checkboxes, Select boxes, Radio buttons,
Concatenation Comments
Arrays: Checkboxes and Textareas
Retrieving information from forms
JavaScript Part 2 Organizing JavaScript Code into Functions
HTML Forms and User Input
Data Types Parsing Data
JavaScript II Retrieving Information from a Form Variables & Data Types Y. Mendelsohn.
JavaScript – Part I Mendelsohn.
Removing events Stopping an event’s normal behavior
JavaScript Variables.
Programming Control Structures with JavaScript Part 2
Training & Development
JavaScript objects, functions, and events
© Hugh McCabe 2000 Web Authoring Lecture 8
Working with ‘checked’ items Checkboxes and Radio buttons
HTML Forms
PHP-II.
Selectors.
Working with Strings.
Events Part III The event object.
Reading information from files
JavaScript Variables.
JavaScript Part 2 Organizing JavaScript Code into Functions
Checkboxes, Select boxes, Radio buttons
JavaScript Variables.
Adios alert() Using innerHTML Using an empty section
Presentation transcript:

Retrieving information from forms JavaScript Retrieving information from forms

Learning Objectives By the end of this lecture, you should be able to: Describe where the information comes from when retrieving from a text box or text area, as opposed to a form element that has a ‘value’ attribute. Comfortably retrieve values from textboxes, text areas, select boxes, and store those values inside variables Do the above without looking at your notes

Retrieving values from a form using JavaScript Consider this simple form: As we have seen, creating this form using HTML code is pretty straight-forward. The next -- and key -- step is to learn how to retrieve the values that were entered into this form (e.g. the value of the two text fields, and the value form the select box) using JavaScript.

There are two ways to retrieve a value In order to retrieve a value from a form, your form element must either: Require the user to enter the value (e.g. via a text box or a text area) In this case, the value we retrieve will be whatever the user has typed into the text box. --- OR --- Include a ‘value’ attribute, when you create the form element In this case, the value comes from form elements in which you (the developer) have included an attribute called ‘value’ , and then you – the programmer—chose a value for that attribute. For example, recall that when we created the <option> tags in our select boxes, we assigned values to them: What is your birth month? <select id="selMonthBorn"> <option value="jan">January</option> <option value="feb">Febuary</option> <option value="mar">March</option> etc... </select>

How to retrieve a value from a form element using JavaScript The syntax is: For example, suppose our form has a text box with an ID of 'txtFirstName'. We could retrieve whatever the user entered in that text box AND store that value inside a variable with the following line of code: var name = document.getElementById('txtFirstName').value; This JavaScript command will: Look for a text field called ‘txtFirstName’ and will retrieve whatever value the user typed inside that text field. The code will then store that value inside the variable called ‘name’. Important Notes: The word ‘document’ will not change during this course. It simply refers to the current web page. The value inside the parentheses must match with one of the ID names in your HTML document. The ‘value’ term says you wish to retrieve the value from that element. If the element you are getting the value of is a text field or text area, then the information you get back will be whatever was typed by the user. If your element has a built-in value attribute (e.g. select boxes), then that is the information that will be retrieved. Example: Retrieving the value from a select box: var monthBorn = document.getElementById('selMonthBorn').value; Will retrieve the value selected by the user from a select box called ‘selMonthBorn’ Suppose that one of the options in that select box was: <option value="feb">Febuary</option> If the user selects that option, then the variable monthBorn will be assigned "feb". document.getElementById("ID-NAME").value;

Retreiving and storing information from a form element Once we retrieve a value from a form, where should we put it? This is one of the countless situations in programming where we use variables. Imagine you have the form shown here: Can you predict what the lines of code are supposed do? Try to figure it out before moving to the next slide. var firstName, lastName; var nationality; var favoriteColor; var aboutUser; firstName = document.getElementById('txtFirstName').value; lastName = document. getElementById('txtLastName').value; favoriteColor = document.getElementById('selFavoriteColor').value; aboutUser = document.getElementById('textarAboutUser').value;

Retreiving and storing information from a form element var firstName, lastName; var nationality; var favoriteColor; var aboutUser; firstName = document.getElementById('txtFirstName').value; lastName = document. getElementById('txtLastName').value; favoriteColor = document.getElementById('selFavoriteColor').value; aboutUser = document.getElementById('textarAboutUser').value; Lines 1-4 are, of course, our variable declarations. Lines 5 and 6 are retrieving the values of text fields. Therefore, the information that gets retrieved will be whatever the user typed into those text fields. Line #7 is retrieving the value of a select box. In this case, the text stored inside the variable favoriteColor will be the information entered inside the value attribute of the particular option tag that was selected by the user. Recall that in this case, the value was set by the web page programmer – NOT the user who is completing the form! Line #8 retrieves whatever text the user typed in a text area. I know it is a text area without looking at the HTML code since, when creating the form, I used the textar prefix. This is just one way in which naming conventions can make it easier to interpret what the code is trying to do.

Pitfall One common source of confusion for students at this point, is not being clear on the distinction between variable names and form element names. Be sure you understand the difference. Form Element Name: When you create a form element, you must give it a name. We give our form elements their names using the 'id' attribute. The reason you give each form element a name is so that you have a way of referring to that element via your JavaScript code. Example: If you had a text box in your form in which the user must indicate how old they are, you would probably give the form element an identifier something like 'txtAge’: <input type="text" id="txtAge"> Variable Name: When you declare a variable, this is an entirely different JavaScript construct. Recall that a JS variable is simply a little piece of storage in which you temporarily hold information. In this talk, we have been using variables to store the information we retrieved from our form. Example: In your script (i.e. as opposed to your HTML form), you might declare a variable to store whatever the user entered for their age: var age = document.getElementById('txtAge').value;

Checked Items When working with checkboxes and radio buttons, we will need to do things a little differently. In order to make them do anything useful for us, we will need to learn how to use “if-else” statements. We will learn how to handle these very soon!

Example js_form_retrieval_part1.html