Upload
chaffey-college
View
36.107
Download
1
Embed Size (px)
DESCRIPTION
Citation preview
Sending Data Using an Online Form
CISIWEB 430A
Objectives In this unit, 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 (create) scalar variables in a script 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
<html><head><title>Chapter 3 - Parsing Data</title></head><body bgcolor="#FFFFFF"><form action="http://myServer/parse2.cgi" METHOD=POST>Comment <input type="text" name="myComment"><p><input TYPE = "submit" value = "Submit"><input TYPE = "reset" VALUE = "Start Over"></p></form></body></html>
#!/usr/bin/perluse CGI qw(:standard);print "Content-type: text/html\n\n";print "<HTML><HEAD><TITLE>Chapter 3";print " - Output</TITLE></HEAD><BODY>\n";print param('myComment');print "</BODY></HTML>";
Planning the script
Plan the HTML page Plan the logic for the script Plan the information the script will
output
Accessing Form Data In an HTML form, the keys are the names of
the form elements, and the values are the information (usually) entered by a user.
When the user presses the “Submit” button, the data are automatically transmitted to the script.
Once the data are sent, our script needs to parse the data so that we can use it. If we use the CGI.pm module, parsing the data
from a form is identical to parsing the data from a hyperlink.
Creating the Form
Use HTML to create the form FORM tag contains instructions
Action = location of .cgi Method = Get or Post Submit – calls the action to happen Controls – include a name attribute
Get vs. Post GET appends the form data to the end of
the URL specified in the action property and is similar to sending the data through a hyperlink
POST sends the data as a separate data stream, allowing the Web server to receive the data through “STANDARD INPUT”
Including special characters
This project uses a dollar sign, which can’t be typed in directly (why not?)
Characters that are given special meaning within a regular expression
Backslash if you want to use literally :. * ? + [ ] ( ) { } ^ $ | \ Any other characters automatically
assume their literal meanings.
Using printf
Printf function is used for displaying data on a Web page Allows you to format the information Allows you to specify the number of decimal
places Allows you to display a + or – before the
number Syntax: printf formatstring, list;
List is a comma separated list of items Formatstring contains text and one or more
format fields
Parts of a format field % - required, at the beginning of a format
field Modifier – optional (+ or – to denote a
positive or negative number) Minimum field width – option, sets a min
number of characters to display Precision – optoin, expressed as a period
followed by a number, the number of digits to the right of the decimal
Format type – required – d (decimal) or f (floating point)