Sending Data Using a Hyperlink CGI/Perl Programming

advertisement
2
Sending Data
Using a Hyperlink
CGI/Perl
Programming
By Diane Zak
1
2
Objectives
• In this chapter, you will:
• Create a hyperlink to a CGI script
• Append data to a URL
• Use the CGI.pm module to parse
data
• Access data using the param
function
2
2
Introduction
• Changes to static web pages can
only be made when the page is
updated on the server
– Requires human intervention
• Changes to dynamic web pages
can be made in response to
information sent through a
hyperlink or form
3
2
Creating a Link to a Script
• Most web pages contain text,
images and hyperlinks
– Hyperlinks or links appear as different
colored text, underlined text, or clickable
images
– The HTML anchor tag <A> with the HREF
property is used to create a link
•
•
•
•
HREF = Hypertext Reference
Syntax: <A HREF=URL>hyperlinktext</A>
hyperlinktext is the clickable text
<A HREF=URL><IMG SRC=imageFile></A> is
4
the syntax for a clickable image
2
Creating a Link to a Script
5
2
Creating a Link to a Script
• When creating a script,
remember to:
– Enter the shebang line (required
for UNIX)
– Enter a comment that includes the
name of the script and the script’s
purpose
– Enter the Content-type line:
• print “Content-type: text/html\n\n”;
6
2
print Function
• Perl can generate HTML output in
various ways:
– print
– printf
– “here” documents
• Syntax for print:
– print output;
• Output can be strings, functions,
variables
• Output can be an expression or a list of
expressions, separated by commas
7
print Function
2
• Using the newline character (\n) can
make HTML code easier to read
when viewing the HTML source in a
browser
– Without \n, the HTML code appears all
on one line, and is difficult to read
• It is good practice to include all of
the HTML tags - HTML, HEAD, and
BODY
8
2
Completing a Script
• Remember that the following
needs to be done for each Perl/CGI
script that is created:
1. Save the document
2. cd path
If UNIX - chmod 755 filename
3. perl -c filename
4. perl -w filename
5. Open the script in a web browser
9
Sending One Item of
Data Using a Link
2
• Append the data to the URL
– Use a question mark (?) to separate the
URL from the data
– Use an equal sign (=) to separate the key
from the value
– Syntax:
<A HREF=URL?key=value>hyperlinkText</A>
– key is the variable name
• One-word name
– value is the value that is assigned to the
key
10
Sending One Item of
Data Using a Link
2
• When you have clicked on a link
that is sending data, the data is
viewable in the browser’s address
box
11
2
Parsing Data
• Data can be sent through a URL,
but the script has to be able to
separate the key from the value
• This ability is part of the
CGI.pm module
– module = collection of prewritten
code stored in a file
– Most modules use the file
extension .pm, which stands for
perl module
12
2
Parsing Data
• Can find if you have cgi.pm by:
– whereis cgi.pm (UNIX)
– Start --> Search --> cgi.pm
(Windows)
– If not, can download from:
• http://www.genome.wi.mit.edu/ftp/pub/
software/WWW/cgi_docs.html
13
2
Parsing Data
• To use CGI.pm, you need to
include the statement:
– use CGI qw(:standard);
• qw = quote words
• Same as the statement use CGI(‘:standard’);
• :standard is an import tag
– Tells the Perl interpreter to allow the use of
the standard features of the CGI.pm module
• Can find out more about CGI.pm, by
typing:
– perldoc CGI.pm
14
2
Parsing Data
• The param function accesses
the value associated with a key
– param is part of the standard
features of CGI.pm
– Syntax:
• param(key) where key is in single or
double quotation marks
• Example:
– param(‘state’);
– param(“state”);
15
2
Parsing Data
• When checking for errors, using
perl -w scriptname, you can:
– Type key=value after the
scriptname
– Example:
• perl -w jackson.cgi state=Alabama
16
2
Parsing Data
• You can also type in key=value
on a separate line
– Example:
• perl -w jackson.cgi
• “(offline mode: enter name=value
pairs on standard input)” message
should appear
• Type in key=value pairs
• Type Ctrl+d (UNIX) or Ctrl+z
(Windows) when done
17
2
Parsing Data
• If the offline mode message
does not appear, the use CGI
statement needs to be changed:
– use CGI qw(:standard -debug);
– This is including the -debug
pragma, and tells CGI.pm to pause
to let you type in the data
• pragma = special type of Perl module.
18
2
Parsing Data
19
2
Parsing Data
20
Sending Multiple Items
of Data Using a Link
2
• When sending more than 1 item of
data through a link, use the following
syntax:
<A HREF=URL?key1=value1&key2=
value2...&keyN=valueN>hyperlinkText</A>
– An ampersand (&) is used to separate
each key and value pair
– Some browsers limit the amount of
information that can be attached to a
URL
21
Sending Multiple Items
of Data Using a Link
2
• When sending a value that contains
more than one word, substitute a plus
sign (+) for a space
– Example:
• <A HREF=“http://yourservername/cgibin/chap02/jackson.cgi?state=Arkansas&cap
=Little+Rock”>Arkansas</A>
• The jackson.cgi script, when sent that data
can get the values of the state and cap keys
using param
– print “The capital of “, param(‘state’), “ is “,
param(‘cap’), “.\n”;
– The capital of Arkansas is Little Rock.
22
Sending Multiple Items
of Data Using a Link
2
23
Summary
2
• The syntax for creating a hyperlink is:
<A HREF=URL>hyperlinkText</A>
– hyperlinkText is the clickable link
• The syntax for creating an image link is:
<A HREF=URL><IMG SRC=imageFile></A>
– imagefile contains the path and name of the
image file
• print output; can be used to generate HTML
instructions.
– output is an expression or list of expressions
24
separated by commas
Summary
2
• To pass one item of data to a script, using a
hyperlink, use the syntax:
<A HREF=URL?key=value>hyperlinkText</A>
– key is the variable name associated with the value
• To pass more than one item of data to a
script, using a hyperlink, separate the key and
value pairs with a &, with the syntax:
<A HREF=URL?key1=value1&key2=value2...&
keyN=valueN>hyperlinkText</A>
• To pass a value that has a space in it, replace
the space with a plus sign (+)
25
Summary
2
• The CGI.pm module can be used to parse
data
– use CGI qw(:standard);
• When testing the script from the command
line, with perl -w, you can either:
– Enter the data on the same line as the command
– Enter each item of data on a separate line
– You may need to use the -debug pragma
• You can use CGI.pm’s param function to
access the value of a key that was passed
to the script
26
Download