PDA

View Full Version : Something wrong with this code


Seawalker0903
03-29-2006, 07:28 PM
Here is the beginners version of what I want to do. I'm still getting a 500 Server error using this method though, (probably something that I'm just not seeing). Also I know there is a better way of doing this, but I'm still kind of a beginner. If someone could either tell me what is wrong with this code or the "better" way of doing it...

#!/usr/bin/perl -w

use CGI;

my $query = new CGI;

$studytype= $query->param('studytype');
$website= $query->param('website');
$familiar= $query->param('familiar');
$webad= $query->param('webad');
$relevant= $query->param('relevant');
$believable= $query->param('believable');
$organized= $query->param('organized');
$design= $query->param('design');
$visual= $query->param('visual');
$contents= $query->param('contents');
$credible= $query->param('credible');
$readable= $query->param('readable');
$links= $query->param('links');

open (LOG, ">> survey.txt");
print LOG "studytype $studytype\n";
print LOG "website $website\n";
print LOG "familiar $familiar\n";
print LOG "webad $webad\n";
print LOG "relevant $relevant\n";
print LOG "believable $believable\n";
print LOG "organized $organized\n";
print LOG "design $design\n";
print LOG "visual $visual\n";
print LOG "contents $contents\n";
print LOG "credible $credible\n";
print LOG "readable $readable\n";
print LOG "links $links\n";
close (LOG);

if($studytype eq "original"){
print $query->redirect('/research/Websites/start.html');
}
else if($studytype eq "info"){
print $query->redirect('/research/Information/start.html');
}
else if($studytype eq "sources"){
print $query->redirect('/research/Sources/start.html');
exit;

FishMonger
03-29-2006, 07:37 PM
When you do a redirect, you need to use the full url.

GENERATING A REDIRECTION HEADER
http://search.cpan.org/~lds/CGI.pm-3.17/CGI.pm#GENERATING_A_REDIRECTION_HEADER

Seawalker0903
03-29-2006, 09:14 PM
I have the full url for the redirect! It would be as follows for each one. Any other ideas?

print $query->redirect('http://www.mydomain.com/research/Information/start.html');

FishMonger
03-29-2006, 09:46 PM
Your syntax is wrong on the else if statements. Here's a cleaned up version.
#!/usr/bin/perl -w

use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);

my $query = new CGI;

open (LOG, ">> survey.txt") || die "Can't open survey.txt $!";

print LOG <<FORM;
studytype $query->param('studytype')
website $query->param('website')
familiar $query->param('familiar')
webad $query->param('webad')
relevant $query->param('relevant')
believable $query->param('believable')
organized $query->param('organized')
design $query->param('design')
visual $query->param('visual')
contents $query->param('contents')
credible $query->param('credible')
readable $query->param('readable')
links $query->param('links')

FORM
close LOG;

if($query->param('studytype') eq "original"){
print $query->redirect('http://www.mydomain.com/research/Websites/start.html');
}
elsif($query->param('studytype') eq "info"){
print $query->redirect('http://www.mydomain.com/research/Information/start.html');
}
elsif($query->param('studytype') eq "sources"){
print $query->redirect('http://www.mydomain.com/research/Sources/start.html');
}
exit;

Seawalker0903
03-30-2006, 01:58 AM
Yeah! It now reads the cgi script and I get log output but it is a mess. Here is what I get:

studytype CGI=HASH(0x811b24c)->param('studytype')
website CGI=HASH(0x811b24c)->param('website')
familiar CGI=HASH(0x811b24c)->param('familiar')
webad CGI=HASH(0x811b24c)->param('webad')
relevant CGI=HASH(0x811b24c)->param('relevant')
believable CGI=HASH(0x811b24c)->param('believable')
organized CGI=HASH(0x811b24c)->param('organized')
design CGI=HASH(0x811b24c)->param('design')
visual CGI=HASH(0x811b24c)->param('visual')
contents CGI=HASH(0x811b24c)->param('contents')
credible CGI=HASH(0x811b24c)->param('credible')
readable CGI=HASH(0x811b24c)->param('readable')
links CGI=HASH(0x811b24c)->param('links')

What I'd like to get is something like this:
studytype 1
website 2
familiar 4
webad 3
relevant 3
believable 2
organized 2
design 3
visual 3
contents 2
credible 1
readable 2
links My links are my favorites.

FishMonger
03-30-2006, 02:49 AM
We can fix that by putting your form submission into a hash.

my $query = new CGI;
my %survey = $query->Vars;

open (LOG, ">> survey.txt") || die "Can't open survey.txt $!";

print LOG <<FORM;
studytype $survey{'studytype'}
website $survey{'website'}
familiar $survey{'familiar'}
webad $survey{'webad'}
relevant $survey{'relevant'}
believable $survey{'believable'}
organized $survey{'organized'}
design $survey{'design'}
visual $survey{'visual'}
contents $survey{'contents'}
credible $survey{'credible'}
readable $survey{'readable'}
links $survey{'links'}

FORM
close LOG;

KevinADC
03-30-2006, 04:46 AM
that's why CGI.pm programmed scripts start with a list of variables, just like the OP had:

$studytype = $query->param('studytype');
.
.
.
$links= $query->param('links');


$query is going to be expanded inside a double-quoted string (and in a <<HERE doc) or you can assign the list to a hash like FishMonger showed.

FishMonger
03-30-2006, 06:18 AM
I sometimes forget that a good percentage of people like to list out a long list of variables. Most of the time I prefer to use the hash, because I believe it's a lot cleaner.

If you want to shorten and clean up your code and don't want to use the hash or here document, you could use a single print statement and pass it a list.

print LOG 'studytype '. $query->param('studytype'), $/,
'website '. $query->param('website'), $/,
'familiar '. $query->param('familiar'), $/,
'webad '. $query->param('webad'), $/,
'relevant '. $query->param('relevant'), $/,
'believable '. $query->param('believable'), $/,
'organized '. $query->param('organized'), $/,
'design '. $query->param('design'), $/,
'visual '. $query->param('visual'), $/,
'contents '. $query->param('contents'), $/,
'credible '. $query->param('credible'), $/,
'readable '. $query->param('readable'), $/,
'links '. $query->param('links'), $/;

close LOG;

KevinADC
03-30-2006, 07:49 AM
The hash is good, especially for this purpose since the hash keys are identical to the lines in the file. You could do something like this:

my $query = new CGI;
my %survey = $query->Vars;
my @names = qw(studytype website familiar webad relevant believable organized design visual contents credible readable links);
open (LOG, ">> survey.txt") || die "Can't open survey.txt $!";
print LOG map {"$_ $survey{$_}$/"} @names;
close LOG;

Seawalker0903
03-30-2006, 05:43 PM
Thanks both of you, it's finally working right! I also learned a lot more than I was getting from the help information!