1 / 22

CGI Programming Part 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. Other Attributes of <input>. name – name of an input field.

tyne
Download Presentation

CGI Programming Part 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CGI Programming Part 2

  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.

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

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

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

  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 <input> • <textarea> - multi-line text field. You can specify the rows and cols attributes. • <select> - creates a drop down menu.

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

  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 <HTML> <HEAD> <TITLE> My Home Page </TITLE> </HEAD> <BODY>

  16. 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”>

  17. end_html • end_html creates the ending part of an HTML document. • For example, print end_html(); creates </body> </html>

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

More Related