Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 CGI Programming Part 2

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.

3 Other Attributes of 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, <input type=“text” name=“street” size = 30> 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, Ms. Mrs. Mr. Dr. 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, reset creates a button to erase all values in a form. For example, 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 - multi-line text field. You can specify the rows and cols attributes. - creates a drop down menu.

9 Handling the Input Input is sent to the CGI program specified in the 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 = $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’}); }

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 ->start_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 My Home Page

16 start_html (cont.) start_html can also take named parameters. Specify attributes to give to the tag. For example, print start_html (-title => “My Title”, -bgcolor => “Red”); creates My Title

17 end_html end_html creates the ending part of an HTML document. For example, print end_html(); creates

18 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.

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

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 user’s 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 to see if file compiles.


Download ppt "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."

Similar presentations


Ads by Google