3 1 Sending Data Using an Online Form CGI/Perl Programming By Diane Zak.

Slides:



Advertisements
Similar presentations
The Web Warrior Guide to Web Design Technologies
Advertisements

Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Tutorial 6 Working with Web Forms
Data types and variables
JavaScript, Fourth Edition
Guide To UNIX Using Linux Third Edition
Chapter 2: Introduction to C++.
JavaScript, Third Edition
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.
Introduction to scripting
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
2 1 Sending Data Using a Hyperlink CGI/Perl Programming By Diane Zak.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
Chapter 2: Using Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
11 1 Cookies CGI/Perl Programming By Diane Zak Objectives In this chapter, you will: Learn the difference between temporary and persistent cookies.
Week 1 Algorithmization and Programming Languages.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
5 1 Data Files CGI/Perl Programming By Diane Zak.
XP Tutorial 8 Adding Interactivity with ActionScript.
Sending data, forms and variables Please use speaker notes for additional information!
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Tutorial 6 Working with Web Forms. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Explore how Web forms interact with.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
HTML Forms. A form is simply an area that can contain form fields. Form fields are objects that allow the visitor to enter information - for example text.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
REEM ALMOTIRI Information Technology Department Majmaah University.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Topics Designing a Program Input, Processing, and Output
Chapter 6 JavaScript: Introduction to Scripting
The Selection Structure
Introduction to Scripting
Intro to PHP & Variables
WEB PROGRAMMING JavaScript.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
JavaScript: Introduction to Scripting
Presentation transcript:

3 1 Sending Data Using an Online Form CGI/Perl Programming By Diane Zak

3 2 Objectives In this chapter, you will: Plan and create a CGI script that processes form data Learn how to prevent Perl from creating undeclared variables in a script Declare and create scalar variables in a script

3 3 Objectives In this chapter, you will: Use assignment statements to assign values to existing variables Send form data to a script using GET and POST Improve the appearance of numbers displayed on a Web page

3 4 Introduction Online forms can also be used to send data to a CGI script, in addition to hyperlinks Online forms are written in HTML Information is sent through a form by clicking on the submit button Typically, after the data has been submitted, a script will create a dynamic Web page that contains an appropriate response, such as: –Acknowledgment –Answer

3 5 Processing Form Data

When the Submit button is clicked, the browser sends the form data to the server 2. The server forwards the form data to the CGI script to process 3. The CGI script sends its output to the server 4. The web server transfers the HTML code to the browser which displays it for the user

3 7 Planning a Script It is important to plan a script beforehand –Errors can be difficult to find Steps to planning: 1. Determine the script’s input and output Input may be the form data 2. List the steps for the script to transform the input into output Algorithm –A set of step-by-step instructions to solve a problem –Written in pseudocode or flowchart 3. Code the algorithm in a language that the computer will understand Like Perl

3 8 Variables in Perl Variable –Location in the computer’s internal memory where a script can temporarily store data –Each variable has a data type Determines the kind of data the variable can store Perl has 3 data types: –Scalar variable – can store one value »Example: number or string –Array variable – can store lists or sets of values –Hash variable – can store lists or sets of values –Each variable has a name

3 9 Variables in Perl Scalar variable naming: –Must begin with dollar sign ($) –Followed by a letter –Optionally followed by one or more letters, numbers, or underscores –Valid names: $city, $inc_tax –Variable names are case-sensitive: $city and $CITY are two different variables –Should use descriptive names

3 10 Variables in Perl Perl does not require variables to be explicitly declared –By default, variables are created “on the fly” Variable exists as soon as you use the variable –It is a good programming practice to not allow variables to be created “on the fly” use strict; –Prevents Perl from creating undeclared variables Syntax to declare variables: –my (variablelist); Example: –my ($hours, $gross, $sales);

3 11 Assignment Statements Can be used to assign or change a value stored in a variable –Interpolation occurs in the fourth example: Perl replaces the variable’s name with the variable’s contents If a variable name occurs within double quotes (“) in a statement, the value of the variable will be replaced for the variable’s name

3 12 Coding the Bonus Calculator Script 4 scalar variables are used: –$name, $sales, $rate, $bonus

3 13 Coding the Bonus Calculator Script The lower precedence number will be performed before a higher precedence number The negation operator is a unary operator – it makes a number negative, rather than the subtraction binary operator which subtracts one number from another

3 14 Coding the Bonus Calculator Script If operators have the same precedence number, the operators are evaluated from left to right Parentheses can be used to change the order that operators are evaluated The modulus operator (%) is used to get the remainder of the division of two integers –Example: 211 % 4 = 3

3 15 Accessing the Values Received from an Online Form Each data item sent by a hyperlink has a key and value Online forms also use keys and values –key = name of the form element –value = data entered by user

3 16 Data sent using an online form must also be parsed by the script –Can use same parsing routing in CGI.pm param function Accessing the Values Received from an Online Form

3 17 The Bonus Calculator Form The tag uses 2 properties: –ACTION The name of the CGI script that will process the form data –METHOD Controls how your web browser sends the form data to the Web server GET –Default method –Appends form data to the end of the URL –Similar to sending data using a hyperlink POST –Sends form data in a separate data stream –Safer than GET »Some web servers limit the size of the URL

3 18 The Bonus Calculator Form

3 19 The Bonus Calculator Form

3 20 Including a Dollar Sign in a Number Use the backslash (\) followed by the dollar sign ($) in a print statement –Backslash is used because the dollar sign has special meaning in Perl (scalar variables)

3 21 Including a Dollar Sign in a Number

3 22 Using the printf function The printf function can be used to format data –Can specify the number of decimal places –Can display a plus sign (+) before positive numbers and a minus sign (-) before negative numbers –Syntax: printf formatstring, list; –formatstring = string that controls the appearance of each item in the list »Can contain text and one or more format fields –list – comma-separated list of items, mostly variables

3 23 Using the printf function 2 format fields are used in the formatstring –%.1f Format the value stored in $avg1 variable Display the value as a floating-point number with 1 decimal place –%.2f Format the value stored in $avg2 variable Display the value as a floating-point number with 2 decimal places

3 24 Using the printf function

3 25 Using the printf function

3 26 Using the printf function in the Bonus Calculator Script

3 27 Summary When you click the submit button, the browser sends the form data to the server. –If the form is associated with a CGI script, the server forwards the form data to the script for processing. –When the script has finished processing, it sends its output – typically HTML – to the server, which transfers the output to the browser. Planning a CGI script involves determining the script’s input, output, and algorithm. A variable is a location, within the computer’s memory, where a script can temporarily store data. –Every variable has a data type and name.

3 28 Summary Perl provides 3 basic data types for variables: –scalar – stores one value – normally number or string –hash –array Scalar variable names begin with dollar sign ($), followed by a letter, and optionally one or more letters, numbers, or underscores. –Variable names are case-sensitive. use strict; can be used to stop Perl from creating variables “on the fly” to force declaration of variables my (variablelist) is used to explicitly declare variables

3 29 Summary An assignment statement is used to assign or change a value of a variable –Syntax: variable = value; When the name of a variable appears within double quotation marks in a statement, Perl replaces the variable’s name with the variable’s contents – interpolation. Single quotation marks around a string indicate that no interpolation should be performed.

3 30 Summary You can use the parsing routine contained in the CGI.pm module to parse the data submitted to a script using an online form. You can use the printf function to format data displayed on a web page. –printf formatstring, list; –formatlist – string that controls appearance of each item in list –list – comma-separated list of items whose values you want to format A format field in a printf function’s formatstring is composed of 5 parts: –%, modifier, minimum field width, precision, and format type –Only % and format type parts are required.