EE 2305 - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

EE 2305

Description:

In this session, you'll get 'up to speed' for completing your ... Review of the param ... that is also used in PERL, such as quote, semi-colon, ampersand, ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 23
Provided by: charle250
Category:
Tags: ampersand

less

Transcript and Presenter's Notes

Title: EE 2305


1
EE 2305
  • Perl CGI Script, Part 3

SMU School of Engineering
2
OutlineIn this session, youll get up to
speed for completing your Website (Chapter
references are from Castro)
  • Review of the param function (Chapter 9)
  • Collecting info from one client page and sending
    it to another client page (Ch. 16)
  • Writing client data to a server file (Ch. 19)
  • Confirming an order via e-mail (Ch. 19)
  • Other capabilities that you may need to complete
    your site.

3
Review FORM Tag Attributes
  • NAME provides a character string that serves as
    an object name for the form
  • ACTION gives the URL location of the CGI (Common
    Gateway Interface) program for submitting data
    collected on the form
  • METHOD specifies the method used to move the data
    on the form from the client to the server. "get"
    causes the form data to be appended to the end of
    the URL

4
Parsing Input from Links or Forms(Review from
2nd PerlCGI Presentation)
  • When a user submits a link or form, the
    name-value pairs are transferred in one long
    stream. When the get method is used, these are
    appended to the URL and sent to the cgi file
    specified in the ACTION parameter
  • To be useful, the stream must be parsed into
    intelligible chunks
  • Use the built-in PerlCGI function param to
    extract information.

5
Using param
  • To get the scalar value of a single variable from
    the submitted form, use the code shown on p. 104.
  • To get the array value of multiple variables from
    the submitted form, use the code on p. 105.
  • Note pp. 106-107 if you want to do more in one
    step.
  • Note that the print commands in the examples
    are only for use in debugging.

6
Collecting info from one page and sending it to
another page (Chapter 16)
  • Remember that Perl is operating in the server,
    and until the user clicks on the shopping cart
    link, the HTML file for the cart is also still in
    the server.
  • This fact is important for understanding the
    tricks you are about to learn.
  • Chapter 16 shows two approaches. We will use the
    Hidden Field approach because it is easier than
    Cookies, and some users may not have Cookies
    enabled in their browser.

7
Using Hidden Fields in a Form
  • Read p. 206
  • Practice making hidden fields in a form so that
    you get the idea, using the example on p. 207.
    Remember that the value of the ACTION parameter
    is whatever name you give to your CGI file in the
    browser. It must have a .cgi extension. You
    dont have to put it in a cgi-bin on SMU servers,
    but you do have to put it in your public_html
    directory.
  • Remember to use the get METHOD.

8
Storing the collected data in the Hidden Field
  • Read pp. 208-209.
  • Note that two scripts are required. One is for
    processing the form that is submitted by the user
    and setting up the hidden fields. Then you need a
    second script for storing the collected data into
    the hidden fields.
  • Study the captions in Figures 16.2-16.6.
  • Note that the names of the files in 16.3 and 16.5
    are hidden1.cgi and hidden2.cgi, respectively.
    This is crucial for getting the transfer from one
    web page to another.

9
Writing Client Data to a Server File
  • After you have captured information about the
    user, his order, and his credit card , you need
    to write this info to a file. See pp. 248-254.
  • Note that the filehandle is a simple way to
    refer to the file.
  • Also, if the file is in your public_html
    directory, you dont need the full path name.
  • The flock function is needed in a real e-commerce
    situation, to allow only one user at a time into
    a file.

10
Confirming a user order via E-mail
  • Pp. 265-6 show how to generate e-mail to your
    customer from within the server.
  • The sendmail program in SMUs server is located
    at usr/lib/sendmail
  • Obviously, the users e-mail address must be one
    of the input items in the client.

11
Miscellaneous Items
  • The following several slides may be useful to
    you, depending on the kinds of things you are
    doing on your website

12
Putting stuff into your HTML from the server
  • The scripts in Castros book all have print
    statements at the end, which are there to help
    you test the functioning of the script.
  • In most cases, these need to be removed from your
    program after you have tested it.

13
Review from Perl CGI, Part 1
  • The primary way to get output from the server
    back to the client is via the PERL print command
  • !/usr/local/bin/perl
  • The following script causes "Stuff" to appear
    on the screen.
  • print "Content-type text/html\n\n"
  • print "Stuff"

14
Formatting Output to the Client HTML
  • After processing is done, and the Content-type
    line is entered,
  • Type print
  • Type "
  • Type ltTAGgt (the name of the HTML tag you want to
    send to the browser)
  • Type the data variables
  • Type the closing tag lt/TAGgt"

15
Note
  • To send a symbol to the client browser, that is
    also used in PERL, such as quote, semi-colon,
    ampersand, etc., place a backslash \ in front of
    it to prevent the PERL processor for getting
    confused
  • Although the ltBRgt tag may be used to create a new
    line in the browsers mark-up, and \r was used in
    JavaScript to force a new line in markup, use \n
    in your CGI script to cause a new line in the
    HTML (page source) to make it easier to read

16
To Print Several HTML Lines at Once
  • This is a special Perl CGI shortcut
  • print ltlt"some label
  • Then type as many HTML lines as you wish.
  • \n not needed. Just remember to \ in front of
  • When you are ready to end the HTML
  • some label

17
Simplifying Paths to Images Links
  • HTML generated by Perl CGI Scripts is not
    actually in your public_html folder
  • To avoid having to use absolute paths for these
  • HTML has a ltBASEgt tag which can be used to tell
    the browser which folder you want to pretend the
    files are in. Inside the ltHEADgt of the HTML,
    print the Perl CGI
  • print "ltBASE HREF\"HTTP//www.engr \"gt"

18
Outputting a Hash as a Table
  • Type print "ltTABLEgt\n" with attributes
  • If table headers are desired print
    "ltTRgtltTHgtKeylt/THgtltTHgtValuelt/THgtlt/TRgt"
  • Type foreach key (keys hashname)
  • Type print "ltTDgtkeylt/TDgt" prints 1st column
  • Type print "ltTDgthashnamekeylt/TDgt" prints 2nd
  • Type (completes foreach loop)
  • Type print "lt/TABLEgt"

19
Outputting an Array as a List
  • Type print "ltOL gt\n" (or ltULgt)
  • Type foreach item(_at_arrayname)
  • Type print "ltLIgtitem\n"
  • Type (completes foreach loop)
  • Complete the list definition
  • print "lt/OLgt" (or lt/ULgt)

20
Formatting Dollars Cents
  • Type printf("\.2f",yourdata) (formats the
    number to 2 decimal places for cents)
  • To pad integers with leading spaces type
  • printf("m.nd",yourdata)
  • m is the total number of spaces and digits
  • n is the number of digits

21
Term Project
  • Be sure to note the Term Project grading criteria
    at http//www.engr.smu.edu/ee/2305
  • Using Perl CGI Script to Capture user input and
    provide a summary back is important, but much of
    the material covered in these three sessions is
    not absolutely necessary. Its embellishment.
  • Also, remember that these three sessions are
    provided only for the term project they will not
    be on the Lecture test or final exam

22
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com