Drag and Drop CGI | 7 | WebReference

Drag and Drop CGI | 7

Advanced ICE Configuration

Search Request Form Configuration

There are two ways to customize the search request form to better match the requirement of your Web site. We've already discussed the first: creating a custom HTML form and using it to collect the search parameters rather than letting the script generate the form. We prefer this method because editing HTML files is simpler and easier than editing HTML generating code embedded within a script. However, for those of you who want your script to generate your input forms, we'll show you where the form is generated within the ice-form.cgi script.

Listing 12.5 shows the Perl subroutines--short pieces of code that perform a specific function--starting at line 80, that generate the request form (see Appendix A for more on Perl subroutines.)


Because configuration of the form generated by the script involves modifying the Perl code, the discussion necessarily uses programming terminology. We don't have space in this chapter to fully define each instance of programmerese. If you find yourself baffled by the instructions we give, here are a couple of things you can do.
  1. Read Appendix A: "Some Notes on Perl for Nonprogrammers." This appendix explains the different elements of a Perl program.
  2. Use deduction. The syntax of the Perl code we discuss in this chapter is quite a bit like English. The structure of the lines of code (indention and blank lines) can also give you clues to where the specific blocks of code start and end.
  3. Ask for help. If you have any programmer friends they should easily be able to figure out what we are trying to convey.
  4. Get a book about Perl. You may want to invest in an introductory Perl text appropriate to your level of programming knowledge.

Listing 12.5 Search request form subroutines

# print the CGI script header
sub send_header {
    local($title)=@_;
    print "Content-type: text/html\n\n";
    print "<HEAD><TITLE>";
    print $title;
    print "</TITLE></HEAD>\n";
}
# display the Forms interface 
sub send_index {
    local($scriptname) = $ENV{"SCRIPT_NAME"};
    print '<BODY>';
    print "<H1>$title</H1>";
    print "<FORM ACTION=\"$scriptname\">\n";
    print <<'END';
  <UL>
  <LI>Enter a keyword or several keywords
   connected with "and" and "or".<BR>
   Example: <I>"picture and binary".</I><P>
   <INPUT NAME="KEYWORDS"  SIZE=50><BR>
  <LI><B>Don't Show</B> documents older than 
   <INPUT NAME="DAYS" VALUE="" SIZE=2>  days, 
   leave this empty to get all documents available.
  <LI><inPUT TYPE="checkbox"
   NAME="THESAURUS" VALUE="thesaurus">   Use <B>Thesaurus</B>
   to extend a search to all synonyms of a term.<BR>
   Example: the query <I>"mail"</I> will return
   <I>"mail"</I> and <I>"message"</I>
  <LI><inPUT TYPE="checkbox" NAME="SUBSTRING"
   VALUE="substring" checked> Use <B>Substring Matching</B> 
   to extend searches to words which contain the given
   term as a substring.<BR>
   Example: the query <I>"mail"</I> will return
   <I>"mail"</I> and <I>"email"</I>
  <LI><B>Choose the Area</B> to search:
   <SELECT name="CONTEXT">
   <OPTION> Search in all documents
END
    foreach $dir (@directories){
      print "<OPTION> $dir\n";
    }
    print <<'END';
  </SELECT>
  <P>
  <LI><B>Start search</B>: <INPUT TYPE="submit" VALUE="Start">
    Reset: <inPUT TYPE="reset" VALUE="Reset"></FORM></OL>
  <P><HR>
  <!-- Please don't delete this message: -->
  <I>This searchable archive was implemented with the 
  <A HREF="http://www.informatik.th-darmstadt.de/~neuss/ice/ice.html">
  ICE search engine </A></I>
  </BODY>
END
}

The send_header subroutine sends the required MIME type identifier to the Web browser along with the <HEAD> and <TITLE> tags. You can modify the Perl print statements to include other tags within the <HEAD> section of the HTML. The send_index subroutine generates the main section of the request form. The first few lines should look familiar.

The print statement is used to generate standard HTML code. You can customize this as you like. To insert a background image, for example, modify the line

print '<BODY>';

to read

print '<BODY BACKGROUND="../images/bkgnd.gif">';

Note the use of single quotation marks around the string and the file path idiom ../ that directs the script to move up a directory (from cgi-bin/) before searching the images/ directory for the image file.


Hair Saver

We've said this before and we'll say it again. Use single quotation marks around text strings unless you want Perl to insert the value of a variable, as in print "$title";

Some characters (\, $, @, %, ‘) have special meanings in Perl and will cause errors or unintended actions if you use them within a double-quoted string. If you need to use one of these in your HTML, use the backslash \ to tell Perl to ignore the special meaning of the following character within a double-quoted string. Perl doesn't try to interpret these characters in a single-quoted string; that's why we tell you to use them.


The next line of interest is

print <<'END';

This is a Perl statement that says ``print everything up to the string END.'' The single quotation marks around 'END' tell Perl to treat the lines that follow as if they, too, are surrounded by single quotation marks, that is, not to interpret special characters. If double quotation marks were placed around the 'END' string, Perl would interpret the special characters.

As you can see, the text between the markers is standard HTML. You can put anything you like between the <<'END' and the END markers that would be legal in a normal HTML file. Don't modify the line that has 'END' on it; the script won't be able to find the end of the block and won't run. Note that there is nothing special about the word END here; we could have used any combination of words. We usually insert a longer string to make it easy to spot in the source file, something like print<<'END_OF_FORM'; with a matching END_OF_FORM at the end of the block.

It's always a good practice to save a copy of your last working script before embarking on modifications. That way, if you go astray you can always start over with a working script.


Comments are welcome

Copyright © 1997 Addison-Wesley Pub Co. and
Created: Oct. 24, 1997
Revised: Oct. 27, 1997

URL: http://webreference.com/dev/dndcgi/search1.html