📄 ch22.htm
字号:
43 {<BR>
44 $snumber = $ssn;<BR>
45 $snumber =~ s/\-//g;<BR>
46 }<BR>
47 elsif ( $ssn =~ /[0-9]{9}/) {<BR>
48 $snumber = $ssn;<BR>
49 }<BR>
50 else {<BR>
51 $error = "Enter the
social security number in the form XXX-XX-XXXX";<BR>
52 }<BR>
53 }<BR>
54 <BR>
55 $fname = $inputs{'fname'};<BR>
56 if ($fname eq "" && error
eq "") {<BR>
57 $error = "Please
enter your first name";<BR>
58 }<BR>
59 <BR>
60 $lname = $inputs{'lname'};<BR>
61 if ($lname eq "" && $error eq "")
{<BR>
62 $error = "Please
enter your last name";<BR>
63 }<BR>
64 <BR>
65 $mname = $inputs{'mname'};<BR>
66 if ($mname eq "" && $error eq "")
{<BR>
67 $error = "Your mother's
maiden name is required";<BR>
68 }<BR>
69 <BR>
70 $dependents = $inputs{'dependents'};<BR>
71 if ($dependents < 1 && $error eq "")
{<BR>
72 print "Now, now, we have
to be dependent on ourselves.";<BR>
73 goto BAILOUT;<BR>
74 }<BR>
75 <BR>
76 #<BR>
77 #<BR>
78 $visa = $inputs{'visa'};<BR>
79 $mastercard = $inputs{'mastercard'};<BR>
80 <BR>
81 if ($error eq "") {<BR>
82 if ($visa eq "" && $mastercard
eq "") {<BR>
83 $error = "At least
pick one card ! ";<BR>
84 }<BR>
85 }<BR>
86 <BR>
87 print <<"HTMLHEAD";<BR>
88 <HTML><TITLE>This is a test</TITLE>
<BR>
89 <BODY><BR>
90 <p><BR>
91 HTMLHEAD<BR>
92 <BR>
93 if ($error eq "")<BR>
94 {<BR>
95 print "\n <H2>Congratulations!</H2>
";<BR>
96 print "<P>Your
application has been accepted";<BR>
97 print "<P>We
will be living off your interest payments shortly";<BR>
98 }<BR>
99 else<BR>
100 {<BR>
101 print "\n <H2>Error!</H2>
";<BR>
102 print "\n <P>$error<P>";
<BR>
103 print "\n <P>Please
correct the error and retry";<BR>
104 }<BR>
105 <BR>
106 #<BR>
107 print <<"HTML";<BR>
108 <p><BR>
109 <A HREF="http://www.ikra.com/credit.html">Restart</A>
<BR>
110 <A HREF="http://www.ikra.com/index.html">Home
Page</A><BR>
111 <p><BR>
112 </BODY></HTML><BR>
113 <BR>
114 HTML<BR>
115 <BR>
116 exit;<BR>
117 <BR>
118 #<BR>
119 # A simple subroutine to briefly test incoming input<BR>
120 #<BR>
121 sub FormArgs {<BR>
122 <BR>
123 if ( $ENV{'REQUEST_METHOD'} eq
"GET" &&<BR>
124 $ENV{'QUERY_STRING'}
ne '') {<BR>
125 $form = $ENV{'QUERY_STRING'};
<BR>
126 $form; # return value is true.
<BR>
127 }<BR>
128 elsif ( $ENV{'REQUEST_METHOD'}
eq "POST" &&<BR>
129 $ENV{'CONTENT_LENGTH'}
ne '0') {<BR>
130 read(STDIN,$form, $ENV{'CONTENT_LENGTH'});
<BR>
131 $form; # return value is true,
continue<BR>
132 } else {<BR>
133 "0"; # Unable to process
<BR>
134 }<BR>
135 <BR>
136 }</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
This script produces the header for the HTML header and body first
with the code in lines 87 through 91. Line 87 asks Perl to print
everything until the string <TT><FONT FACE="Courier">HTMLHEAD</FONT></TT>
is found by itself on a line. Line 88 starts a new HTML page,
followed by the start of the body of the page with the <TT><FONT FACE="Courier"><BODY></FONT></TT>
tag, and then a blank line with the <TT><FONT FACE="Courier"><P></FONT></TT>
tag. Note that I did not use the <TT><FONT FACE="Courier"><TITLE></FONT></TT>
and <TT><FONT FACE="Courier"></TITLE></FONT></TT> tag pair.
<BLOCKQUOTE>
<TT><FONT FACE="Courier">print <<"HTMLHEAD";<HTML><TITLE>This
is a test</TITLE><BR>
<BODY><BR>
<p><BR>
HTMLHEAD</FONT></TT>
</BLOCKQUOTE>
<P>
Then, the script examines the <TT><FONT FACE="Courier">$error</FONT></TT>
string to see if it had any problems listed in it. If no problems
are seen (that is, the <TT><FONT FACE="Courier">$error</FONT></TT>
string is empty), then the script accepts this input and prints
out an acknowledgment. On the other hand, if there are some problems,
then the script prints out the value of <TT><FONT FACE="Courier">$error</FONT></TT>
to show what the errors are and print those out instead.
<P>
At this point, the script can write out HTML tags and text for
sending back the content of an HTML page to the browser. Regardless
of what the result of <TT><FONT FACE="Courier">action</FONT></TT>
is, you have to close out the HTML output with the <TT><FONT FACE="Courier"></BODY></FONT></TT>
and <TT><FONT FACE="Courier"></HTML></FONT></TT> tags. Then
you are done. The response is sent back to the browser, and you
can safely exit.
<P>
Perl gives you, as a programmer, enormous flexibility and power
to control how you handle responses and echo back messages. I
used the construct <TT><FONT FACE="Courier">print << "HTML"</FONT></TT>.
Anything from that statement on will be printed to <TT><FONT FACE="Courier">STDOUT</FONT></TT>
(standard output of the script or until the end of file), until
either that <TT><FONT FACE="Courier">exit</FONT></TT> statement
or the word <TT><FONT FACE="Courier">HTML</FONT></TT> is found
by itself on one line.
<H2><A NAME="UsingtheCollectedData"><B><FONT SIZE=5 COLOR=#FF0000>Using
the Collected Data</FONT></B></A></H2>
<P>
So far, you've been able to collect the incoming data from the
user and verify that it is correct for the HTML <TT><FONT FACE="Courier">FORM</FONT></TT>
you are supporting. Now the question is what can you do with the
collected data? Well, basically anything you want, because it's
local to your script now. Two of the most common actions you might
want to take with this data is to archive it to disk or mail the
contents as a message to someone.
<H3><A NAME="ArchivingUserResponses"><B>Archiving User Responses</B></A>
</H3>
<P>
The archival process to store the incoming data can be done in
many different ways. You can use the incoming name and other information
to store values in a text string or a database. Using the techniques
covered in <A HREF="ch18.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch18.htm" >Chapter 18</A>, "Databases
for Perl," you can construct your own database. At the very
least, you can archive the responses in a plain text file by appending
them to an existing file.
<P>
A simple solution is to use the following lines to write them
all out. It'll be one long text file.
<BLOCKQUOTE>
<TT><FONT FACE="Courier">open (MYARchIVE,"applicants",
0666) || die "Cannot open Archive";<BR>
print MYARchIVE "SOR";<BR>
print MYARchIVE "$inputs{'income'} ";<BR>
print MYARchIVE "$inputs{'ssn'} ";<BR>
print MYARchIVE "$inputs{'lname'} ";<BR>
print MYARchIVE "$inputs{'dependants'}";<BR>
print MYARchIVE "$inputs{'mastercard'} ";<BR>
print MYARchIVE "$inputs{'mname'} ";<BR>
print MYARchIVE "$inputs{'fname'} ";<BR>
print MYARchIVE "EOR";<BR>
close MYARchIVE;</FONT></TT>
</BLOCKQUOTE>
<P>
Using a crude method like this might get you by if you have only
a few applicants. The <BR>
appended, plain text file might be hard to manage and search.
This method for storing user responses certainly won't handle
multiple applications by the same individual. What you probably
want to do is to store the information in some sort of database
file. Look at <A HREF="ch18.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch18.htm" >Chapter 18</A> for information
on using free databases generated by DBM (the database management
utilities supplied with Perl) or using commercial database applications
such as Oracle, Sybase, Informix, and so on, which you can access
from within a Perl script using the DBI (database interface).
<P>
For a commercial application, you're better off using an existing
database from Oracle, dBASE, or some other commercial database
management system. With a commercial system you're able to use
the DBI to take advantage of particular features of that database.
<P>
Perl comes with several modules, including GDBM, NDBM, and SDBM.
For the purpose of illustration, they are functionally the same,
so I'll use GDBM. This will help keep the focus on how to handle
data from within a CGI script, rather than going off into a tutorial
on databases.
<P>
In this script, you'll use the <TT><FONT FACE="Courier">GDBM_File.pm</FONT></TT>
module with the following line:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">use GDBM_File;</FONT></TT>
</BLOCKQUOTE>
<P>
All Perl modules end with the <TT><FONT FACE="Courier">.pm</FONT></TT>
extension; the <TT><FONT FACE="Courier">use</FONT></TT> command
does not require that this be specified. To see if you have the
module in your system, look in the <TT><FONT FACE="Courier">/usr/lib/perl5</FONT></TT>
directory for all the files ending <TT><FONT FACE="Courier">*.pm</FONT></TT>.
<P>
Next, you have to figure out how to store the users' responses
in this database. An almost-unique key for this application is
the user's <TT><FONT FACE="Courier">$ssn</FONT></TT> field. Perhaps
you can create the index by concatenation of the <TT><FONT FACE="Courier">$ssn</FONT></TT>
field with the last name (<TT><FONT FACE="Courier">$lname</FONT></TT>).
<BLOCKQUOTE>
<TT><FONT FACE="Courier">$appIndex= $lname . $ssn;</FONT></TT>
</BLOCKQUOTE>
<P>
Using this <TT><FONT FACE="Courier">$appIndex</FONT></TT> variable,
you can index into your sample database, which is called <TT><FONT FACE="Courier">applicants.dbm</FONT></TT>.
Create this database first and then associate it with the <TT><FONT FACE="Courier">%applicants</FONT></TT>
associative array. That way, if the applicant has already applied
for credit, you can give him or her an error message or proceed
with updating his or her information. The action to take is really
up to you. The following snippet of code shows how to use DBM
to track applicants:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">dbmopen(%applicants,"applicants.dbm",
0666);<BR>
if ($applicants{$appIndex} eq "") {<BR>
$error = "You have already applied for credit. We get back
to you";<BR>
}<BR>
else {<BR>
$applicants{$appIndex}= $form;<BR>
}<BR>
dbmclose(%applicants);</FONT></TT>
</BLOCKQUOTE>
<P>
Basically, you are saving the query string in <TT><FONT FACE="Courier">$form</FONT></TT>
for future use. Any other script reading the <TT><FONT FACE="Courier">applicants.dbm</FONT></TT>
file will have to break this string apart to get the individual
words, just like in the <TT><FONT FACE="Courier">credit.pl</FONT></TT>
script.
<H3><A NAME="F
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -