Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Perl CGl Programming: No experience required.
(Publisher: Sybex, Inc.)
Author(s): Erik Strom
ISBN: 0782121578
Publication Date: 11/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Notice that the HTML code sets up a two-column table with its width restricted to 50 percent of the screen. The VALIGN="top" tag is used to ensure that each column lines up properly in the display.


TIP:  Why isn’t the HTML code within the here portion of the print statement indented to line up with the Perl code? When you use print’s here operator, the function interprets everything that follows literally, so any spaces or tabs you use to make everything line up and look pretty will also be printed. The here operator is so useful that you will soon learn to overlook its aesthetic drawbacks.

With the exception of the last line in the previous code snippet, which starts an <H3> heading to show the date and time of the visit, the code that formats the date and time is unchanged. One more small addition at the bottom finishes the modifications to search.pl to bring it to the Web:

   # Set the $Found flag and clean up the HTML.

               $Found = 1;
               print "</H3>";
               }            # End if ($InfoArray…)
           }                # End while (read…)
   # If the $Found flag is still 0, put up a message.

       if (!$Found)
           {
           print "<H2 ALIGN=\"CENTER\">Sorry. No matches found for
           $search.\n";
           print "</H2>\n";
           }

   #                    End search.pl

Install the new search.pl where your Web server expects to find CGI applications, then try it out. Using the last search example in search.html, you’ll see a page similar to the one displayed in Figure 12.4.


Figure 12.4:  The search results displayed on a Web page


TIP:  You can add a neat touch to the display by slightly changing the line of code that prints the visitor’s e-mail address. Instead of $InfoArray[$EMail], make it <A HREF=\"mailto: $InfoArray[$EMail]\"> $InfoArray[$EMail] </A>. That way, anyone viewing the search results can click on the link to send mail to this visitor to your Web site.

Writing a More Complex Search

Even though the search results look a lot better now that they’re displayed in HTML, the look-up capabilities of search.html and search.pl are still anemic at best. For example, what would happen in the quite likely case that two or more people in the guest book have the same last name? The way the search engine is built now, as a loop that goes through the entire database looking for and displaying matches, all of them would pop up in the results. Is there some way to refine the search, to make it more specific?

Certainly there is. Because of the way we structured the information in the guest book file, every field in the original guest book form could be included in the criteria for the search. We won’t make it that complicated, but it would be nice to be able to couple first and last names in searching for a visitor.

To accomplish this, you’ll need to add another field to the form in search.html. The new form is created with the following HTML code:

   <HTML>

   <HEAD>
   <TITLE>Search the Guest Book</TITLE>
   </HEAD>

   <BODY>
   <H1 ALIGN="CENTER">Search the Guest Book</H1>
   <HR>
   <FORM ACTION="/cgi-bin/search.pl" METHOD="GET">
   <CENTER>
   <H3>Search for: </H3>
   <STRONG>Last name </STRONG>
   <INPUT TYPE="text" NAME="last_name" SIZE="24" MAXLENGTH="30"> <BR>
   <STRONG>First name </STRONG>
   <INPUT TYPE="text" NAME="first_name" SIZE="24" MAXLENGTH="30">
   <P>
   <INPUT TYPE="submit" VALUE="Submit search">
   <INPUT TYPE="reset" VALUE="Clear form">
   </P>
   </FORM>
   </BODY>
   </HTML>

Called up in your Web browser, the page will look similar to the one displayed in Figure 12.5.


Figure 12.5:  Searching for a visitor using first and last names

The new HTML form has two fields to fill in; how should search.pl handle it if only one (or none) of them is filled when the form gets submitted? As usual, there are a couple of ways to go about it. You could decide that neither the first- nor last-name fields can be empty and put up an error message; or you could treat an empty field as a wildcard, allowing it to match anything.

In the latter case, entering a last name and leaving the first-name field blank would give the same result as the first version of search.pl, putting up a list of every guest book entry with that last name. This method sounds friendlier than scolding people for not completely filling out the form—how can we go about implementing it?

First, you’re now picking up a full query string from the form, so you’ll have to split it into its components and clean the components up as you did for the guest book entry script in Skill 7. Modify the code near the top of search.pl to the following:

   # Get the search term through CGI and split it up.

       $SearchString = $ENV{'QUERY_STRING'};
       @SearchTerms = split (/&/, $SearchString);

   # Massage the URL encoding out of the terms, strip off the
   # variable names and make any empty search terms global
   # with "*".

       for ($n = 0; @SearchTerms[$n]; $n++)
           {
           ($dummy, $temp) = split (/=/, @SearchTerms[$n]);
           $temp =~ tr/+/ /;
           $temp =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
           if ($temp eq "")
               {
               $temp = ".*";
               }
           @SearchTerms[$n] = $temp;
           }
           .
           .
           .

This is essentially what you did in addguest.pl, with one exception. Notice the conditional expression:

   if ($temp eq "")
       {
       $temp = ".*";
       }

This expression tests whether $temp is equal to "", which is an empty string. Why set it to ".*" if it’s empty? Because you want a blank field to match anything in the guest book database now. In a regular expression, the period matches any character and the asterisk matches zero or more occurrences of it. So, ".*" will match anything.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.