CGI Programming Part 2 - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

CGI Programming Part 2

Description:

CGI Programming Part 2. Input Tags. Many different ways of getting data from the user. ... select - creates a drop down menu. Handling the Input ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 23
Provided by: csUt8
Category:

less

Transcript and Presenter's Notes

Title: CGI Programming Part 2


1
CGI Programming Part 2
2
Input Tags
  • Many different ways of getting data from the
    user.
  • The ltinputgt tag is used most often.
  • ltinputgt has a type attribute
  • Specifies the method with which to get data from
    the user.

3
Other Attributes of ltinputgt
  • name name of an input field.
  • value value of an input field.
  • size width of an input field.
  • maxlength maximum number of characters that can
    be entered in an input field.
  • checked whether a radio or checkbox is turned
    on.
  • src url of an image.

4
Input Type Text
  • Setting the type to text creates a text box.
  • For example,
  • ltinput typetext
  • namestreet
  • size 30gt
  • Note, name does not appear in the webpage.
  • It is seen only by the CGI program.

5
Input Type Radio
  • Setting the type to radio creates a radio button.
  • For example,
  • ltinput typeradio nametitle valuems
    checkedgt Ms. ltbrgt
  • ltinput typeradio nametitle valuemrsgt
    Mrs. ltbrgt
  • ltinput typeradio nametitle valuemrgt Mr.
    ltbrgt
  • ltinput typeradio nametitle valuedrgt Dr.
    ltbrgt
  • All related radio buttons have the same name.
  • The values are all preset.

6
Input Type Submit and Reset
  • submit creates a submit button to contact a CGI
    program.
  • For example,
  • ltinput typesubmit valueSubmit Formgt
  • reset creates a button to erase all values in a
    form.
  • For example,
  • ltinput typereset valueClear Formgt
  • The value appears in the button.

7
Input Type Others
  • checkbox creates a checkbox
  • password creates a password field. A text field
    with characters displayed as .
  • button creates a button the user can press.
  • file creates a field to upload a file.
  • image creates an image the user can click to
    submit a form.

8
Inputs Other Than ltinputgt
  • lttextareagt - multi-line text field. You can
    specify the rows and cols attributes.
  • ltselectgt - creates a drop down menu.

9
Handling the Input
  • Input is sent to the CGI program specified in the
    ltformgt tag using either the get or post method.
  • It is best to write CGI programs that handle
    both.
  • Can be done by examining the environment
    variable, REQUEST_METHOD.

10
An Example
  • request_method ENVREQUEST_METHOD
  • if (request_method eq GET)
  • query_string ENVQUERY_STRING
  • elsif (request_method eq POST)
  • read(STDIN,
  • query_string,
  • ENVCONTENT_LENGTH)

11
CGI.pm
  • Many parts of CGI programs are repeated.
  • Makes sense to capture these parts for reuse.
  • CGI.pm is a module of Perl functions to do many
    common tasks.
  • Many functions provide shortcuts to create HTML.
  • Module can be used as a collection of functions
    or an object oriented class.

12
Object-Oriented Use
  • Can be used as an object-oriented class by
  • use CGI
  • q new CGI
  • Access subroutines as methods of q
  • For example,
  • print q -gtstart_html()

13
Function-Oriented Use
  • Import a set of functions to be called directly.
  • non-object oriented is often faster than
    object-oriented.
  • Use a quoted list to tell Perl which functions to
    import.
  • use CGI (start_html, end_html, header)
  • CGI.pm also defines sets of these functions.
    standard and html3 are the most common.
  • use CGI qw(standard html3)

14
header
  • The header function creates the HTTP header.
  • The default is text/html.
  • For example,
  • print header()
  • creates
  • print Content-type text/html\n\n

15
start_html
  • start_html create the HTML header
  • start_html can take one parameter, the title.
  • For example,
  • print start_html(My Home Page)
  • creates
  • ltHTMLgt
  • ltHEADgt
  • ltTITLEgt My Home Page lt/TITLEgt
  • lt/HEADgt
  • ltBODYgt

16
start_html (cont.)
  • start_html can also take named parameters.
  • Specify attributes to give to the ltbodygt tag.
  • For example,
  • print start_html (-title gt My Title,
  • -bgcolor gt Red)
  • creates
  • lthtmlgt
  • ltheadgt
  • lttitlegt My Title lt/titlegt
  • lt/headgt
  • ltbody bgcolor Redgt

17
end_html
  • end_html creates the ending part of an HTML
    document.
  • For example,
  • print end_html()
  • creates
  • lt/bodygt
  • lt/htmlgt

18
param
  • param gets the parameter values sent to a CGI
    program.
  • Query String
  • nameMax20Powersjobceo
  • Can get the query values by
  • name param(name)
  • job param(job)
  • This is the scalar context.

19
Contexts of param
  • Can be called in list context with no arguments.
    Returns names of all parameters.
  • e.g. _at_params param()
  • Can be called in list context with an argument.
    Returns an array of all values for that argument.
  • e.g. _at_vals param(title)

20
Locking Files
  • Many webpages allow multiple users to access the
    same information.
  • i.e. multiple user writing to the same file.
  • Need a mechanism to ensure consistency.
  • Do not want one users action to undo the actions
    of another.
  • A file must be locked when being written to.

21
flock
  • flock can be used to lock a file.
  • The syntax is
  • flock (filehandle, mode)
  • Set mode to
  • 2 to lock a file.
  • 8 to unlock a file.
  • For example,
  • flock (FILE, 2)

22
Debugging
  • Debugging a CGI script can be frustrating.
  • Browser will display 500 Internal Server Error
    if any errors occur.
  • Some useful tips
  • Run the program from the command line.
  • Use perl c ltfilenamegt to see if file compiles.
Write a Comment
User Comments (0)
About PowerShow.com