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.
Forms and Form Controls Chapter What is a Form?
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.
XHTML Introductory1 Forms Chapter 7. XHTML Introductory2 Objectives In this chapter, you will: Study elements Learn about input fields Use the element.
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,
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
HTML FORMS GET/POST METHODS. HTML FORMS HTML Forms HTML forms are used to pass data to a server. A form can contain input elements like text fields, checkboxes,
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
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
JavaScript Syntax, how to use it in a HTML document
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.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
Event-Driven Programming Chapter Page Section: Section or division of an HTML page Generic block element Example: What's the difference between.
Creating Web Page Forms COE 201- Computer Proficiency.
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.
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.
HTML Tutorial. What is HTML HTML is a markup language for describing web documents (web pages) HTML documents are described by HTML tags Each HTML tag.
Client-side (JavaScript) Validation. Associating a function with a click event – Part 1 Use the input tag’s onclick attribute to associate a function.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
JavaScript Part 1 Introduction to scripting The ‘alert’ function.
JavaScript: Conditionals contd.
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
Checkboxes, Select boxes, Radio buttons,
Concatenation Comments
Arrays: Checkboxes and Textareas
>> Form Data Retrieval in JavaScript
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
HTML Forms and User Input
Data Types Parsing Data
JavaScript – Part I Mendelsohn.
JavaScript Variables.
Training & Development
JavaScript: Introduction to Scripting
© Hugh McCabe 2000 Web Authoring Lecture 8
Working with ‘checked’ items Checkboxes and Radio buttons
HTML Forms
Selectors.
Events Part III The event object.
Reading information from files
JavaScript Variables.
JavaScript Part 2 Organizing JavaScript Code into Functions
Retrieving information from forms
Checkboxes, Select boxes, Radio buttons
Adios alert() Using innerHTML Using an empty section
<div> <span>
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. Retrieve values from textboxes, text areas, select boxes, store that information in variables.

Information you can obtain from a form In order to retrieve text, your form element must either: Require the user to enter text (e.g. via a text box or a text area) In this case, the value we retrieve will be whatever the user has typed in the text box. --- OR --- Have a ‘value’ attribute In this case, the value comes from form elements in which you (the developer) created a ‘value’ attribute and assigned it a value. For example, recall that when we created the <option> tags in our select boxes, we assigned a value to them: What month were you born in? <select id="selMonthBorn"> <option value="jan">January</option> <option value="feb">Febuary</option> <option value="mar">March</option> etc... </select>

Retreiving information from a form element The syntax is: document.getElementById('ID-NAME').value; For example, supposed we had a text box with an ID of 'txtFirstName'. We could retrieve whatever the user entered in that box with the following line of code: var name = document.getElementById('txtFirstName').value; This will retrieve whatever the user typed inside a text field with the id ‘txtFirstName’ and will store it in the variable called ‘name’. 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 value attribute (e.g. select boxes), then that is the information that will be retrieved. Example: Retrieving the value from a select box: var month_born = 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 month_born will be assigned "feb".

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. Can you predict what the following lines of code will do? Try to figure it out before moving to the next slide. var firstName, lastName; var nationality; var favoriteColor; firstName = document.getElementById('txtFirstName').value; lastName = document. getElementById('txtLastName').value; favoriteColor = document.getElementById('selFavoriteColor').value; comments = document.getElementById('textarComments').value;

Retreiving and storing information from a form element var firstName, lastName; var nationality; var favoriteColor; firstName = document.getElementById('txtFirstName').value; lastName = document. getElementById('txtLastName').value; favoriteColor = document.getElementById('selFavoriteColor').value; comments = document.getElementById('textarComments').value; Lines 1-3 are, of course, our variable declarations. Lines 4 and 5 are retrieving the values of text fields. Therefore, the information retrieved will be whatever the user entered. Line #6 is retrieving the value of a select box. In this case, the text stored by the variable will be the information entered inside the ‘value’ attribute of the particular option tag that was selected by the user. Line #7 retrieves whatever text the user typed in a text area. I know it is a text area without looking at the HTML code since I used the 'textar' prefix. Just one way in which naming conventions can help you more easily interpret what your code is doing.

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 of something like to '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 work for us, we will need to learn how to use “if-else” statements. This will come up 1-2 modules down the road.

File See ‘js_form_retrieval.htm’