cgi programming part 2

22
CGI Programming Part 2

Upload: tyne

Post on 15-Jan-2016

60 views

Category:

Documents


1 download

DESCRIPTION

CGI Programming Part 2. Input Tags. Many different ways of getting data from the user. The tag is used most often. has a type attribute Specifies the method with which to get data from the user. Other Attributes of . name – name of an input field. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CGI Programming Part 2

CGI Programming Part 2

Page 2: CGI Programming Part 2

Input Tags

• Many different ways of getting data from the user.

• The <input> tag is used most often.

• <input> has a type attribute – Specifies the method with which to get data

from the user.

Page 3: CGI Programming Part 2

Other Attributes of <input>• 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.

Page 4: CGI Programming Part 2

Input Type: Text

• Setting the type to text creates a text box.

• For example,

<input type=“text”

name=“street”

size = 30>

• Note, name does not appear in the webpage.

• It is seen only by the CGI program.

Page 5: CGI Programming Part 2

Input Type: Radio

• Setting the type to radio creates a radio button.

• For example,<input type=“radio” name=“title” value=“ms” checked> Ms. <br>

<input type=“radio” name=“title” value=“mrs”> Mrs. <br>

<input type=“radio” name=“title” value=“mr”> Mr. <br>

<input type=“radio” name=“title” value=“dr”> Dr. <br>

• All related radio buttons have the same name.

• The values are all preset.

Page 6: CGI Programming Part 2

Input Type: Submit and Reset

• submit creates a submit button to contact a CGI program.

• For example,<input type=“submit” value=“Submit Form”>

• reset creates a button to erase all values in a form.

• For example,<input type=“reset” value=“Clear Form”>

• The “value” appears in the button.

Page 7: CGI Programming Part 2

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.

Page 8: CGI Programming Part 2

Inputs Other Than <input>

• <textarea> - multi-line text field. You can specify the rows and cols attributes.

• <select> - creates a drop down menu.

Page 9: CGI Programming Part 2

Handling the Input

• Input is sent to the CGI program specified in the <form> 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.

Page 10: CGI Programming Part 2

An Example$request_method =

$ENV{‘REQUEST_METHOD’};

if ($request_method eq “GET”) {$query_string = $ENV{‘QUERY_STRING’};

}elsif ($request_method eq “POST”) {

read(STDIN, $query_string, $ENV{‘CONTENT_LENGTH’});

}

Page 11: CGI Programming Part 2

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.

Page 12: CGI Programming Part 2

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 ->start_html();

Page 13: CGI Programming Part 2

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);

Page 14: CGI Programming Part 2

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”;

Page 15: CGI Programming Part 2

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

<HTML><HEAD><TITLE> My Home Page </TITLE></HEAD><BODY>

Page 16: CGI Programming Part 2

start_html (cont.)• start_html can also take named parameters.• Specify attributes to give to the <body> tag.• For example,

print start_html (-title => “My Title”, -bgcolor => “Red”);

creates<html><head> <title> My Title </title></head><body bgcolor = “Red”>

Page 17: CGI Programming Part 2

end_html

• end_html creates the ending part of an HTML document.

• For example,

print end_html();

creates

</body>

</html>

Page 18: CGI Programming Part 2

param

• param gets the parameter values sent to a CGI program.

• Query String:name=Max%20Powers&job=ceo

• Can get the query values by:$name = param(“name”);

$job = param(“job”);

• This is the scalar context.

Page 19: CGI Programming Part 2

Contexts of param

• Can be called in list context with no arguments. Returns names of all parameters.– e.g. @params = param();

• Can be called in list context with an argument. Returns an array of all values for that argument.– e.g. @vals = param(“title”);

Page 20: CGI Programming Part 2

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 user’s action to undo the

actions of another.

• A file must be locked when being written to.

Page 21: CGI Programming Part 2

flock

• flock can be used to lock a file.

• The syntax isflock (filehandle, mode);

• Set mode to – 2 to lock a file.– 8 to unlock a file.

• For example,flock (FILE, 2);

Page 22: CGI Programming Part 2

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 <filename> to see if file compiles.