![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
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.
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, youll see a page similar to the one displayed in Figure 12.4.
Writing a More Complex SearchEven though the search results look a lot better now that theyre 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 wont 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, youll 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.
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 formhow can we go about implementing it? First, youre now picking up a full query string from the form, so youll 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 its 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.
|
![]() |
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. |