Gossamer Forum
Home : General : Perl Programming :

Perl Experts Please Help, Adding an Item check box to Perl Script???

Quote Reply
Perl Experts Please Help, Adding an Item check box to Perl Script???
Hello,

I am trying to add a checkbox with multiple items that can be selected from a form to show up on a page once selected... I have it sort of working but only one of the selected checked items shows up instead of the multiple selected items... I am sort of new working with perl scripts so I'm not sure if I have this right, I'm kinda lost, please advise...


ok, here's what I have so far; I've posted it in sections...

--------------------------------------------

# These are the interests categories for the user pages.

$interests_1 = "Arts & Crafts";
$interests_2 = "Community Service";
$interests_3 = "Dancing";
$interests_4 = "Dining";
$interests_5 = "Family";
$interests_6 = "Movies";
$interests_7 = "Music";
$interests_8 = "Outdoor Activities";
$interests_9 = "Photography";
$interests_10 = "Religion/Spiritual";
$interests_11 = "Sports";
$interests_12 = "Theater";
$interests_13 = "Travel";

-------------------------------------------
# This is the checkbox of interests for the main page.

print "<TABLE CELLPADDING=1 CELLSPACING=2 BORDER=0>\n<TR>\n";
print "<p><select name=\"interests\" size=\"1\">\n";
print "<TD VALIGN=TOP><P>\n";
print "<INPUT TYPE=CHECKBOX NAME=\"interests\" VALUE=\"$interests_1\"> Arts & Crafts<BR>\n";
print "<INPUT TYPE=CHECKBOX NAME=\"interests\" VALUE=\"$interests_2\"> Community Service<BR>\n";
print "<INPUT TYPE=CHECKBOX NAME=\"interests\" VALUE=\"$interests_3\"> Dancing<BR>\n";
print "<INPUT TYPE=CHECKBOX NAME=\"interests\" VALUE=\"$interests_4\"> Dining<BR>\n";
print "<INPUT TYPE=CHECKBOX NAME=\"interests\" VALUE=\"$interests_5\"> Family<BR>\n";
print "<INPUT TYPE=CHECKBOX NAME=\"interests\" VALUE=\"$interests_6\"> Movies<BR>\n";
print "<INPUT TYPE=CHECKBOX NAME=\"interests\" VALUE=\"$interests_7\"> Music<BR>\n";
print "</TD>\n";
print "<TD VALIGN=TOP><P>\n";
print "<INPUT TYPE=CHECKBOX NAME=\"interests\" VALUE=\"$interests_8\"> Outdoor Activities<BR>\n";
print "<INPUT TYPE=CHECKBOX NAME=\"interests\" VALUE=\"$interests_9\"> Photography<BR>\n";
print "<INPUT TYPE=CHECKBOX NAME=\"interests\" VALUE=\"$interests_10\"> Religion/Spiritual<BR>\n";
print "<INPUT TYPE=CHECKBOX NAME=\"interests\" VALUE=\"$interests_11\"> Sports<BR>\n";
print "<INPUT TYPE=CHECKBOX NAME=\"interests\" VALUE=\"$interests_12\"> Theater<BR>\n";
print "<INPUT TYPE=CHECKBOX NAME=\"interests\" VALUE=\"$interests_13\"> Travel<BR>\n";
print "</TD>\n";
print "</blockquote>\n";
print "</TR>\n";
print "</TABLE>\n";

--------------------------------------------
sub preview_html {

print "<center><strong><em>$user_input{'interests'}</strong></em></center>\n";

}

---------------------------------------------

Thank You
Quote Reply
Re: Perl Experts Please Help, Adding an Item check box to Perl Script??? In reply to
all you need is a parse form that looks like this:

Code:
sub parse_form {
my (@pairs, %in);
my ($buffer, $pair, $name, $value);

if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
}
elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
}
else { print "You can't run this from telnet" and die; }

PAIR: foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
($value eq "---") and next PAIR;
exists $in{$name} ? ($in{$name} .= "~~$value") : ($in{$name} = $value);
}
return %in;
}

and then when you output it..

Code:
foreach (split("~~", $in{'interests'})) {
print "$_, ";
}