helpanovice has asked for the wisdom of the Perl Monks concerning the following question:

#Working copy is located at www.swosu.ed/util/directry.htm
#!/home/bin/ # This script generates a HTML FORM, with itself as the "ACTION" # When this script is called it checks the STDIN for data it may # be passing to itself. If there is none, it generates the FORM # plus some help text on how to use it. If it does see search # keys in STDIN ($keys) it will again print the FORM, skip the # help text, open the data file, search for matches, and print # them as a HTML table. #require("cgilib.pl"); #****************************************************************** #**************** Main Rtn *********************** #****************************************************************** print "Content-Type: text/html\n\n"; $phone="/home/docs/resources/data/swosufac.dat"; print("<HEAD>\n"); print("<TITLE>Results</TITLE></HEAD>\n"); print("<BODY TEXT=\"#000000\" BGCOLOR=\"#FFFFFF\">\n"); print("<center>\n"); print("<img src=\"/images/swbanner.jpg\" usemap=\"#header\" border=\"0 +\" ALT=\"SWOSU\">\n"); print("</center>\n"); &GET_STATE_INFO; print "<center>\n"; print "<H1>Faculty/Staff Directory Look Up</H1>\n"; print "</center>\n"; print "<P>Enter Search Keys</P>\n"; print "<FORM METHOD=POST ACTION=/cgi-perl/swosufac.pl>\n"; print "<INPUT TYPE=\x22text\x22 NAME=\x22keys\x22 SIZE=40 "; print "VALUE=\x22", $keys, "\x22>\n"; print "<INPUT TYPE=\x22submit\x22 Name=\x22submit\x22 Value =\x22Searc +h\x22>\n"; print "</FORM>\n"; if ( $keys eq "") { print "<HR>"; print "<B>Enter Search words separated by spaces, Search words will +be"; print " AND'ed together (only those names that contain all the keys" +; print " will be returned.)</B>"; print "<P>"; print "The Name field will be searched.<P>"; } else { print "<HR>"; print "<B>Search Results</B>"; print "<TABLE BORDER>\n"; print "<TH ALIGN=\x22center\x22>Name</TH>\n"; print "<TH ALIGN=\x22center\x22>Office</TH>\n"; print "<TH ALIGN=\x22center\x22>Phone Number</TH>\n"; print "<TH ALIGN=\x22center\x22>Title</TH>\n"; print "<TH ALIGN=\x22center\x22>Email Address</TH>\n"; print "<TH ALIGN=\x22center\x22>HomePage</TH><TR>\n"; &SEARCH_FOR_MATCH($keys); print "</TABLE>\n"; } print "<P>"; print "<hr>\n"; print "<I>Please send comments or suggestions to <A HREF=\"mailto:webm +aster\@swosu.edu\">Webmaster</a></i>\n"; print "<p>\n"; print "<font size=\"1\">Southwestern Oklahoma State University<br>\n"; print "100 Campus Drive<br>\n"; print "Weatherford, OK 73096<br>\n"; print "<p>\n"; print "<MAP NAME=\"header\">\n"; print "<AREA SHAPE=RECT COORDS=\"234,39,358,58\" HREF=\"/index.htm\" A +LT=\"SWOSU Home\">\n"; print "<AREA SHAPE=RECT COORDS=\"362,37,417,61\" HREF=\"/util/search.h +tm\" ALT=\"Search SWOSU\">\n"; print "<AREA SHAPE=RECT COORDS=\"418,39,478,59\" HREF=\"/sitemap.htm\" + ALT=\"SWOSU Site Map\">\n"; print "<AREA SHAPE=RECT COORDS=\"483,38,599,61\" HREF=\"/util/directry +.htm\" ALT=\"Campus Directory\">\n"; print "<AREA SHAPE=\"default\" NOHREF=\"NOHREF\">\n"; print "</MAP>\n"; print("</BODY>\n"); print("</HTML>\n"); #****************************************************************** #******************** Subroutines ******************************** #****************************************************************** #****************************************************************** #**************** Get Passed data from form *********************** #****************************************************************** sub GET_STATE_INFO { %fields = (); read(STDIN, $save_string, $ENV{CONTENT_LENGTH}); # Yes- Use it @prompts = split(/&/,$save_string); foreach (@prompts) { ($tmp1, $tmp2) = split(/=/,$_); $tmp2 =~ s/\x2b/\x20/g; $tmp2 =~ s/%2C/\x2c/g; $tmp2 =~ s/%28/\x28/g; $tmp2 =~ s/%29/\x29/g; $fields{$tmp1}=$tmp2; } $keys = $fields{'keys'}; $keys = "\U$keys"; } #****************************************************************** #********** Search file for lines with all search keys ************ #****************************************************************** sub SEARCH_FOR_MATCH { @search_key = split(/\x20/,$keys); $k =0; open(MYFILE, $phone); while(<MYFILE>) { $in_line = $_; ($name, $office, $phone_num, $title,$email,$hmpg ) = split(/~/,$in_l +ine); if ($email ne "") { $email="<a href=\"mailto:" . $email . "\@swosu.edu\" >" . $email . " +\@swosu.edu </a>"; } if ($hmpg ne "") { $hmpg="<a href=\"\/perl\/disclaim.pl\?\/\~" . $hmpg . "\/\">" . $hmp +g . "</a>"; } $found = "yes"; foreach (@search_key) { $pos_out = rindex("\U$name",$_); if ($pos_out < 0) { $found = "no"; } } if ( $found eq "yes") { if ($k > 999) { next; } # limit to 999 matches $k = $k + 1; print "<TD ALIGN=\x22left\x22>", $name, "</TD>\n"; print "<TD ALIGN=\x22left\x22>", $office, "</TD>\n"; print "<TD ALIGN=\x22left\x22>", $phone_num, "</TD>\n"; print "<TD ALIGN=\x22left\x22>", $title, "</TD>\n"; print "<TD ALIGN=\x22left\x22>", $email, "</TD>\n"; print "<TD ALIGN=\x22left\x22>", $hmpg, "</TD>\n"; print "<TR>\n"; } } }

Replies are listed 'Best First'.
Re: Netscape 4.7 and down won't run this script, IE, NS6 - works fine
by japhy (Canon) on Jan 22, 2001 at 22:21 UTC
    Your HTML output is totally disgusting. You have TH elements outside of TR elements, and you never close your TR elements. Try creating better HTML.

    japhy -- Perl and Regex Hacker
Re: Netscape 4.7 and down won't run this script, IE, NS6 - works fine
by Trinary (Pilgrim) on Jan 22, 2001 at 22:30 UTC
    Ok, I haven't taken a serious look at this, or tested it (yet), but I *garuntee* that your code will look better, work better and be easier to understand if you use CGI.pm for all your form stuff, and probably a decent chunk of your plain HTMl. The amount of things you can do wrong drops to a huge degree when you use well understood and tested modules. I know it's easy to try to do everything yourself when you have a basic understanding of language concepts, but one of the best things about Perl is the vast array of modules ready and free for use. Check it out, use what others have done before you, and watch your efficiency and ability rise. =)

    Trinary

try: man CGI
by stefan k (Curate) on Jan 22, 2001 at 22:23 UTC
    As far as I can see, it shouldn't run in whatever way you start it because of the first line:
    #!/home/bin/
    which gives no appropriate interpreter. Furthermore when I take a quick glance at the code, I get the strong feeling you should have a close look at the CGI-Module (just try man CGI in any terminal (I assume that you're running some kind of unix from the path in your code's first line). That will help you a lot!
    HaveFun
    Stefan K
    $dom = "skamphausen.de"; ## May The Open Source Be With You! $Mail = "mail@$dom; $Url = "http://www.$dom";
Re: Netscape 4.7 and down won't run this script, IE, NS6 - works fine
by arturo (Vicar) on Jan 22, 2001 at 22:33 UTC

    Well, kids, we have a doozy here: it turns out that (according to our chums over at netcraft), we've got mod_perl going here:

    The site www.swosu.edu runs Apache/1.3.12 (Unix) mod_perl/1.21 on Linux

    I'm out of my depth here, but it seems to me that the problem could be in a lot of places. It at least explains why the script runs even with that odd she-bang (#!) line.

    It strikes me that PerlSendHeaders might be on in mod_perl, which could help explain why the code looks the way it does.

    Things I'd offer for a start: use CGI in this script, both to get the CGI variables and to spit out the HTML.

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: Netscape 4.7 and down won't run this script, IE, NS6 - works fine
by wardk (Deacon) on Jan 23, 2001 at 03:11 UTC

    I haven't perused your code, but make sure you are forming your HTML tables correctly ( there is mention of misplaced table tags in previous responses), netscape will not render a table with a missing end table tag where IE will assume the ending </table>. perhaps NS6 makes such assumptions.

    misplaced TR/TH/TD tags are a sure way to thwart page rendering under NS

    good luck!

Re: Netscape 4.7 and down won't run this script, IE, NS6 - works fine
by helpanovice (Initiate) on Jan 23, 2001 at 03:55 UTC
    Okay, Got both of my directory searches working properly. Turns out, the PerlSendHeader in the config had been deleted. With that back in, Netscape recognizes the print "Content-Type: text/html\n\n"; and IE now does not display that line of text at the top.

    Now the only Perl script left to fight is my Perlfect search, which again runs fine in internet explorer but not in Netscape.

    www.swosu.edu/util/search.htm

    CD - (helpanovice + some more experience at the end of the day!)
      Your close table tag isn't. Change it to </table> and you'll be swell. NE is particular about it's tables. And I'm guessing the META content-type = ... isn't doing much. IE and NE have different ways of determining content type; IE ignores the content type tag, NE pays orea attention, but you've got it after the <HTML> tag, so it may be too late.

      a

        no, meta-tags can't change much since the content-type is already sent with the http-header (which, in normal case, you create all by yourself when writing perl-cgis). And a meta-tag in that form doesn't exist anyway. The correct version is (e.g.):
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
        (xhtml compatible..)

        If using tables you should be extremly carefull. NS has some really weird behavior, e.g. when you give a columns width in percent you can't center it anymore, etc.. (there some ways to work around, but i had that problem several times..) Try using CGI.pm or close ALL tags.. It doens't matter if you have too many closing tags, but it matters if you don't have enough..

        Regards, octopus

        --
        GED/CC d-- s:- a--- C++(+++) UL+++ P++++$ L++>++++ E--- W+++@ N o? K? w-- O- M-(+) V? !PS !PE !Y PGP+(++) t-- 5 X+ R+(+++) tv+(++) b++@ DI+() D+ G++ e->+++ h!++ r+(++) y+
        Sometimes you overlook the obvious don't you?

        Things are wonderful - I blame the Monday bug for all the problems.