Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "3 1 Sending Data Using an Online Form CGI/Perl Programming By Diane Zak."— Presentation transcript:

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

2 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 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

4 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

5 3 5 Processing Form Data

6 3 6 1. 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

7 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

8 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

9 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

10 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);

11 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

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

13 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

14 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

15 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

16 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

17 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

18 3 18 The Bonus Calculator Form

19 3 19 The Bonus Calculator Form

20 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)

21 3 21 Including a Dollar Sign in a Number

22 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

23 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

24 3 24 Using the printf function

25 3 25 Using the printf function

26 3 26 Using the printf function in the Bonus Calculator Script

27 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.

28 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

29 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.

30 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.


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

Similar presentations


Ads by Google