3 Sending Data Using an Online Form CGI/Perl Programming By Diane Zak 1 3 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 2 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 3 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 4 – Answer 3 Processing Form Data 5 3 Processing Form Data 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 6 to the browser which displays it for the user 3 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 7 3 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 8 3 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 9 3 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); 10 3 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 11 replaced for the variable’s name 3 Coding the Bonus Calculator Script • 4 scalar variables are used: – $name, $sales, $rate, $bonus 12 3 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 13 3 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 14 3 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 15 3 Accessing the Values Received from an Online Form • Data sent using an online form must also be parsed by the script – Can use same parsing routing in CGI.pm • param function 16 3 The Bonus Calculator Form • The <FORM> 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 17 » Some web servers limit the size of the URL 3 The Bonus Calculator Form 18 3 The Bonus Calculator Form 19 3 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) 20 3 Including a Dollar Sign in a Number 21 3 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 22 variables 3 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 23 decimal places 3 Using the printf function 24 3 Using the printf function 25 3 Using the printf function in the Bonus Calculator Script 26 Summary 3 • 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. 27 – Every variable has a data type and name. Summary 3 • 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 28 Summary 3 • 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. 29 Summary 3 • 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 30 – Only % and format type parts are required.