Forum Moderators: coopster & phranque

Message Too Old, No Replies

Problem "writing" checkbox selections to flat file dbase

only shows one selection

         

Howard Beach

1:05 am on Jul 13, 2005 (gmt 0)



Hi,

I'm working on a form submission script where the user fills out the form by checking off the best ten poems out of twenty five. The twenty five poem titles are part of one checkbox field called "poem". When the user checks off their top ten poems they are taken to a "Review your Submission" screen, where their top ten poems are displayed, then they click SUBMIT. Everything works fine here but when I open the database file that the submission gets written to only the first of their ten poems selected gets saved to the dbase.

Does anyone know why this is happening and what I can do to fix it?

The checkbox field is called "poem".

Following is the .pl file if this will help:

#!/usr/bin/perl
#######################################################

#Turn off Perl buffering
$¦ = 1;

#Read and parse the form data
&ReadParse(*input);

#Assign variables

$date = `$date_command +"%m\/%d\/%Y"`;
$date =~ s/\n*$//;
$current_date = $date;
$test = 0;
$time = time;
$cardlist_temp = "$cardlist" . "_" . "temp";

if ($input{'send'} ne "") {
print "Location: $url_of_postcard_html", "\n\n";
}
elsif ($input{'pickup'} ne "") {
print "Location: $url_of_pickup_html", "\n\n";
}
elsif ($input{'preview'} ne "") {
print &PrintHeader;
&check_required;
&select_card_number;
&print_preview;
}
elsif ($input{'submit'} ne ""){
print &PrintHeader;
&send_card;
&delete_old_cards;

}
elsif ($ENV{'QUERY_STRING'} ne ""){
print &PrintHeader;
&pickup_card;
}

#########################################################################
sub delete_old_cards{
$current_time = time;

$delete_date = $current_time - ($days * 86400);

open (TEMP, ">$cardlist_temp") ¦¦ die ("Could not open $cardlist_temp");
flock (TEMP, 2);
open (CARD, "$cardlist") ¦¦ die ("Could not open $cardlist");
flock (CARD, 2);
while (<CARD>){
($number, $from, $from_email, $message, $to, $notify, $image, $date_time, $date) = split(/\¦/, $_);
if ($delete_date < $date_time){
print TEMP "$_";
}
}
flock (CARD, 8);
flock (TEMP, 8);
close (TEMP);
close (CARD);
rename ($cardlist_temp, $cardlist);
}

############################################################
sub check_required{
if ($input{'address'} eq ""){
print "You must enter your email address so that a ";
print "confirmation can be e-mailed to you. Please use the back button ";
print "on your browser, enter your e-mail address and then submit the form again. ";
exit;
}
unless ($input{'address'} =~ /^[\w-.]+\@[\w-.]+$/){
print "Invalid email address. Please use the back button on your browser to ";
print "enter the correct email address and then submit the form again. ";
print "Thank you.";
exit;
}
if ($input{'sender'} eq ""){
print "Please enter your Poet ID number. ";
print "Please use the back button on your browser to enter your Poet ID number ";
print "and then submit the form again. Thank
you.";
exit;
}
if ($input{'sender'} eq "") {
print "You must specify your name in order for this ";
print "form to be submitted. Please use the back button on your browser to ";
print "enter your name and then submit the form again. Thank you.";
exit;
}
if ($input{'notify'} ne "" && $input{'senderemail'} eq ""){
print "You must enter your email address in order to be notified when your form ";
print "is picked up. Please use the back button on your browser to ";
print "enter your email address or deselect the notification checkbox and then ";
print "submit the form again. Thank you.";
exit;
}
if ($input{'senderemail'} ne ""){
unless ($input{'senderemail'} =~ /^[\w-.]+\@[\w-.]+$/) {
print "Invalid email address. Please use the back button on your browser to ";
print "enter the correct email address and then submit the form again. ";
print "Thank you.";
exit;
}
}
}

############################################################
#Preview postcard
sub print_preview{
open (PAGE, "$preview_postcard") ¦¦ die ("I am sorry, but I was unable to open the
file $preview_postcard.");
while (<PAGE>){
s/%%date%%/$date/g;
s/%%name%%/$input{'name'}/g;
s/%%sender%%/$input{'sender'}/g;
s/%%message%%/$input{'message'}/g;
s/%%address%%/$input{'address'}/g;
s/%%senderemail%%/$input{'senderemail'}/g;
s/%%yourtel%%/$input{'yourtel'}/g;
s/%%poem%%/$input{'poem'}/g;
s/%%message%%/$input{'message'}/g;
s/%%notify%%/$input{'notify'}/g;
s/%%image%%/$image/g;
print $_;
}
close (PAGE);
}

############################################################
#Put in the correct photograph
sub select_card_number {
$image = $input{'card_number'};
}

############################################################
sub send_card{
#Assign a userid
srand (time¦$$);
$userid = int (rand(1000));
$userid .= "$$";

#Print out thank you message
open (PAGE, "$thank_you") ¦¦ die ("I am sorry, but I was unable to open the file
$thank_you.");
while (<PAGE>){
s/%%name%%/$input{'name'}/g;
print $_;
}
close (PAGE);

#Write form data to file
open (DATABASE, ">>$cardlist") ¦¦ die ("I am sorry, but I was unable to open the file.");

#Remove any pipe symbols from input
$input{'sender'} =~ s/\¦//g;
$input{'message'} =~ s/\¦//g;
$input{'name'} =~ s/\¦//g;
$input{'senderemail'} =~ s/\¦//g;
$input{'message'} =~ s/\s+/ /g;
$input{'address'} =~ s/\s+/ /g;
$input{'yourtel'} =~ s/\s+/ /g;
$input{'poem'} =~ s/\s+//g;

flock (DATABASE, 2);

print DATABASE "$userid\¦$input{'poem'}\¦$input{'message'}\¦$input{'sender'}\¦$input{'address'}\¦$input{'yourtel'}\¦ $input{'senderemail'}\¦$input{'name'}\¦$input{'notify'}\¦$input{'card_number'}\¦$time\¦$date\¦$input {'address'}\n";

flock (DATABASE, 8);
close (DATABASE);

#Send e-mail to recipient
open (MAIL, "¦$mailprog $input{'address'}") ¦¦ die "Can't open $mailprog!\n";
print MAIL "Reply-to: $input{'senderemail'}\n";
print MAIL "From: $input{'senderemail'}\n";
print MAIL "To: $input{'address'}\n";
print MAIL "Subject: $subject\n\n";
open (PAGE, "$mail_message") ¦¦ die ("I am sorry, but I was unable to open the file
$thank_you.");
while (<PAGE>){

s/%%name%%/$input{'name'}/g;
s/%%sender%%/$input{'sender'}/g;
s/%%message%%/$input{'message'}/g;
s/%%date%%/$input{'date'}/g;
s/%%address%%/$input{'address'}/g;
s/%%senderemail%%/$input{'senderemail'}/g;
s/%%poem%%/$input{'poem'}/g;
s/%%message%%/$input{'message'}/g;
print MAIL $_;
}
close (PAGE);
close (MAIL);
}

############################################################
sub pickup_card {
#Open card.list file
open (DATABASE, "$cardlist") ¦¦ &CgiDie ("I am sorry, but I was not able to open the
data file.");
flock (DATABASE, 2);
#Assign database to list array
@list = <DATABASE>;
flock (DATABASE, 8);
close (DATABASE);

#Split array into scalar variables
foreach $list (@list) {
($userid, $sender, $senderemail, $message, $name, $notify, $image, $date_time, $date) = split
(/\¦/, $list);

#Compare form input to $userid
if ($input{'id_num'} eq $userid && $input{'id_num'} ne "") {
$test = 1;
&print_html;
if ($notify eq "on") {
&notify;
}
last;
}
}

if ($test == 0) {
&invalid_id;
}
}
############################################################
sub print_html {
open (PAGE, "$show_postcard") ¦¦ die ("I am sorry, but I was unable to open the file
$thank_you.");
while (<PAGE>){
s/%%sender%%/$sender/g;
s/%%date%%/$date/g;
s/%%name%%/$name/g;
s/%%message%%/$message/g;
s/%%image%%/$image/g;
print $_;
}
close (PAGE);
}

############################################################
#Send e-mail to sender
sub notify {
$date =~ s/\n*$//;
open (MAIL, "¦$mailprog $senderemail") ¦¦ die "Can't open $mailprog!\n";
print MAIL "Reply-to: $reply_to\n";
print MAIL "From: $reply_to\n";
print MAIL "To: $senderemail\n";
print MAIL "Subject: Postcard Picked-up\n\n";
print MAIL "The postcard you sent to $name, from $company, was picked up on ";
print MAIL "$date. \nThank you.";
close (MAIL);
}

############################################################
#Invalid user message
sub invalid_id {
print <<EOT;
Sorry, but you have entered an invalid card ID number. Please use the back button
on your browser to re-enter the correct card ID.
EOT
exit;
}

rocknbil

3:21 am on Jul 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The twenty five poem titles are part of one checkbox field called "poem".

This is your problem. Although I can't see your ReadParse, I'm presuming it's CGI.pm or libhtml.pl, in either case you would get poem.1, poem.2, etc, if you looked.

Radio buttons should be named the same name as single-selections, but checkboxes should have unique names. I'd do this like

name="poem_1"...
name="poem_2"...

then

foreach $v (keys %input) {
if ($v =~ /poem\_/) { push @poemlist, $input{$v}); }
}

$input{'poem'} = join(',',@poemlist);

That way you'd only have to add that foreach loop.

But you should pay close attention that this is very, very bad

open (MAIL, "¦$mailprog $input{'address'}") ¦¦ die "Can't open $mailprog!\n";

The pipe can be used very maliciously, for everything from emailing a default-located password list to using your script to mega-spam. Use the -t option, which gathers the email address to send to from the mail text.

open (MAIL,"¦ $mailprog -t") ¦¦ &error("Cannot start $mailprog $!");
print (MAIL "To: $to\r\n");
print (MAIL "From: $from\r\n");
.......

(I prefer an error output sub)

But that's just how I would do it, TMTOWTDI, good luck. :-)