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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



alexz
stranger

Jul 30, 2001, 11:47 AM

Post #1 of 4 (1736 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:
<html><head></head><body>
<form action="search.cgi" method="post">
Enter the words to search for:
<input type="text" name="name" size=30>
<input type="submit" value="search">
</form>
</body</html>

cgi code:
#!/usr/bin/perl
$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'};

open(INF,$datafile);
@mydata = <INF>;
close(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)
{
chomp($i);
($num,$name,$status,$price) = split(/\|/,$i);
print "<b>$name</b> (#$num) - \$$price
\n";
}
}
else
{
print "No results found.<p>\n";
}
print "</body></html>\n";





mhx
Enthusiast / Moderator

Jul 30, 2001, 10:46 PM

Post #2 of 4 (1726 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:

Code
#!/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;
exit;
}

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


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



alexz
stranger

Jul 31, 2001, 7:03 AM

Post #3 of 4 (1722 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.



mhx
Enthusiast / Moderator

Jul 31, 2001, 7:55 AM

Post #4 of 4 (1720 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


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"


 
 


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

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