CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
scratch the bottom post, more problems



Jul 30, 2001, 11:47 AM

Post #1 of 4 (1756 views)
scratch the bottom post, more problems Can't Post

i need help writing a perl cgi script that would ask the user which keyword he/she would like to search for. then it would search a couple text files to see if the keyword is there and then the output would be if the keyword was present in the file or not. so far this is what i have, but as of now, its only searching one text file. and also, it doesnt work. any help would be greatly appreciated.

html code:
<form action="search.cgi" method="post">
Enter the words to search for:
<input type="text" name="name" size=30>
<input type="submit" value="search">

cgi code:
$datafile = "data.txt";
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/~!/ ~!/g;
$FORM{$name} = $value;

$searchstr = $FORM{'name'};

@mydata = <INF>;
print "Content-type:text/html\n\n";
print "<html><head><title>Search Results</title></head>\n";
print "<body><h3>Search Results</h3>\n";

@results = grep(/$searchstr/i,@mydata);
if ($results >= 0)
foreach $i (@results)
($num,$name,$status,$price) = split(/\|/,$i);
print "<b>$name</b> (#$num) - \$$price
print "No results found.<p>\n";
print "</body></html>\n";

Enthusiast / Moderator

Jul 30, 2001, 10:46 PM

Post #2 of 4 (1746 views)
Re: scratch the bottom post, more problems [In reply to] Can't Post

Try this as a starting point. It should work well with your HTML form:

#!/bin/perl -wT 
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use strict;

unless( param() && param('name') ) {
print header, start_html('Error'), h1('Invalid request!'), end_html;

my $datafile = 'data.txt';
my $pattern = quotemeta param('name');

open FILE, $datafile or die "cannot open $datafile: $!\n";
my $data = do { local $/=undef; <FILE> };
close FILE;

print header, start_html('Results');
if( $data =~ /$pattern/ ) {
print h1("$pattern found in database!");
else {
print h1("$pattern not found in database!");
print end_html;

Have a look around in this forum for some general tips on CGI script writing.
Hope this helps.

-- Marcus



Jul 31, 2001, 7:03 AM

Post #3 of 4 (1742 views)
Re: scratch the bottom post, more problems [In reply to] Can't Post

thanks for the code, but there is a problem. when i execute the search from the html page, i just get a screen that shows my code. im unfamiliar with cgi, and im trying to read as much about it, but in case i dont find the info im looking for, can u tell me how i can get the cgi to correctly run? thanks a lot.

Enthusiast / Moderator

Jul 31, 2001, 7:55 AM

Post #4 of 4 (1740 views)
Re: scratch the bottom post, more problems [In reply to] Can't Post

Do you have CGI support on your webserver?
Where do you put your script? There's a dedicated directory on the server, in most cases called cgi-bin, that cannot be accessed using HTTP but only by the webserver. Only files in this directory, if they meet certain criteria (that are defined by the server configuration) are interpreted as CGI scripts / programs. If you say that your code appears, it seems that it isn't is such a directory.
So, try to find out more about if you do have CGI support and if you do how your directory structure looks like. If you have more detailed questions, feel free to ask.

-- Marcus



Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives