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:
Guestbook Multiple Checkboxes

 



Revival Fire
Novice

Jan 6, 2001, 11:58 AM

Post #1 of 6 (4650 views)
Guestbook Multiple Checkboxes Can't Post

I am trying to get a CGI to post multiple checkbox entries on a guestbook. It only lists the last checked checkbox and not all of them. Can anybody help me change the Perl in the CGI to process all the information? Here is the HTML and the CGI....


The HTML

<b>Please select the services you would like to order:</b>




<input type="checkbox" name="service" value="I want a Directory Listing">Five-Line Directory Listing

<input type="checkbox" name="service" value="I want a Directory Listing">Additional lines on Directory Listing

<input type="checkbox" name="service" value="I want a Category Banner Ad">Category Banner Ad


<input type="checkbox" name="service" value="I want a Homepage Button Ad">Homepage Button Ad


<input type="checkbox" name="service" value="I want a Email Newsletter Ad">Email Newsletter Ad


<input type="checkbox" name="service" value="I want a Graphic">Graphic Construction For Button or Banner Ad


<input type="checkbox" name="service" value="I want a Webpage Built">Build a Webpage


<input type="checkbox" name="service" value="Other Services">Other Services (detailed below)
<p>



the CGI
(Private Info Removed of Course)

#!/usr/bin/perl
$|=1;
# bnbbook.cgi: yes, another guest book script....
# Release 1.0 on 09/06/98
# (C) 1998 BigNoseBird.Com, Inc. This program is freeware and may
# be used at no cost to you (just leave this notice intact).
# Feel free to modify, hack, and play with this script.
# This guestbook (like the world really needs another one)
# has borrowed several ideas from the works of Selena Sol
# (http://www.extropia.com/) and Matt Wright
# (http://cgi-resources.com/). The script is the result of user
# requests for something smaller and simpler to work with, but
# with some new tricks.
#
##################################################################
# START USER CONFIGURATION SECTION #
##################################################################
# For information on formatting your autoresponse letter and #
# guestbook format, please read the README.TXT #
# For information on formatting your input form, also see the #
# gbook.html file included in this distribution. #
##################################################################
#
# SPECIAL RESERVED HTML FORM NAMES
# When designing your HTML input form for your guest book,
# there are only four fields that are handled in a special way
# by the script. The are:
#
# signer_email: the e-mail address of the signer of the book. If
# you want to have the script autorespond or be able
# to easily reply to them- use this name!
# private: Value is YES if it is a private message not to
# be shown in the book. You will get e-mail.
# required: A comma delimited list of "must-fill" fields.
# If the user does not complete any field you
# specify, they will get a message to go back.
# url: The person's homepage URL. This will be presented
# in the guest book as an HTML link.
#
##################################################################

# set $HTML="NO" if you do not want users to be able to enter HTML tags
# the form name "private" when set to YES by a reader, if you offer
# the choice, will send you e-mail, but will not write to the guestbook.

$HTML="YES";

# $GUESTBOOK is the file name for your guestbook file. You must give the
# filename including it's full path.

$GUESTBOOK="/scsi3/main/www..com//.html";

# $GUESTBOOK_URL is the URL of the guestbook. This way after they
# sign the book they are redirected back to it

$GUESTBOOK_URL="http://www..com/Thanks.html";

# $TEMPDIR is a directory on your server where you have permission to
# write files that will be deleted when the script finishes running.

$TEMPDIR="/tmp";

# $SEND_THANKS if "YES" will send the signer of the book a thank you
# note if they supplied a valid e-mail address. E-mail is sent to
# the value contained in "signer_email"

$SEND_THANKS="YES";

# $MY_EMAIL should be your e-mail address. This way people know where
# they get the thank you not from. Be sure to put that \@ backslash
# before the AT sign!

$MY_EMAIL="Info\@.com";

# If you make $TELL_ME="YES" and supplied a value for $MY_EMAIL,
# the script will notify you when somebody has signed your book.
# If you generally do not want to recieve e-mail, except in the
# case of a private message, let $TELL_ME="NO"

$TELL_ME="YES";

# $MAIL_PROGRAM is your system's e-mail program typically either
# /usr/lib/sendmail -t or /usr/sbin/sendmail -t
# DON'T FORGET THE "-t", omitting it is a major source of script failure

$MAIL_PROGRAM="/usr/lib/sendmail -t";

# For Windows 95/98/NT using BLAT.EXE, it might look like this:
# $MAIL_PROGRAM="C:/winnt/system32/blat.exe";
# You will find more information on Windows modifications you will
# have to make in the subroutines, notify_me and send_thanks.

# $MUNG="YES" will transform e-mail addresses into something a little
# less spam-spider friendly. <evil grin>

$MUNG="NO";

# @CENSORED is an array of words that you do not want to have
# appear on your guestbook.

@CENSORED=(,);

# $VALID_DOMAIN if set is where the script can be called from. If
# your site responds with or without the "www", leave off the www!

$VALID_DOMAIN=".com";

sub setup_thankyounote
{
$THANK_YOU=<<__END_OF_THANK_YOU__;
$fields{'title'} $fields{'name'},

Thank you for submitting your website to .
We are glad to have you become a part of the web directory.

God Bless You,

Founder and President


...taking Christian eCommerce to a new level...the glory of God!
http://www..com




---You wrote----

Company: $fields{'company'}
Contact: $fields{'title'} $fields{'name'}
E-Mail: $fields{'signer_email'}
Address: $fields{'address'}
City, State, & Zip: $fields{'city'}, $fields{'state'} $fields{'zip'}
Phone $fields{'phone'}
Phone listed? $fields{'phone_number_listed?'}
Country: $fields{'country'}
Service $fields{'service'}
Discount $fields{'Discount'}
Home Page: $fields{'url'}
Cat. & Sub-cat.: $fields{'category'}/ $fields{'sub-category'}
Description: $fields{'message'}
Extra...
How Found Us: $fields{'howfound'}
Linked From: <a href="$fields{'linked_from'}">$fields{'name_of_site_linked_from'}</a>
Questions/Comments: $fields{'comments'}


__END_OF_THANK_YOU__
}

##################################################################
sub setup_pageentry
{
$tzn=$fields{'signer_email'};
if ($MUNG eq "YES")
{
$tzn =~ s/\./_DoT_/g;
$tzn =~ s/\@/_AT_/g;
}

$PAGE_ENTRY=<<__END_OF_PAGE_ENTRY__;
<center>
<p>
<h3>
Display For: $fields{'company'}


Submitted On: $the_date
<p>
</h3>
<font size="-2">
\n";
print WRBK "$PAGE_ENTRY\n";
}
else
{
print WRBK "$line\n";
}
}
close(WRBK);
&drop_the_lock;
}

##################################################################
sub get_the_lock
{
$lockfile="$TEMPDIR/bnbbook.lck";
local ($endtime);
$endtime = 60;
$endtime = time + $endtime;
while (-e $lockfile && time < $endtime)
{
# Do Nothing
}
open(LOCK_FILE, ">$lockfile");
}

##################################################################
sub drop_the_lock
{
close($lockfile);
unlink($lockfile);
}


##################################################################
sub valid_address
{
if ($fields{'signer_email'} eq "")
{
$BAD_EMAIL_FORMAT="YES";
return;
}
$testmail = $fields{'signer_email'};
if ($testmail =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/ ||
$testmail !~ /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/)
{
$BAD_EMAIL_FORMAT="YES";
return;
}
else
{
$BAD_EMAIL_FORMAT="NO";
}
}

##################################################################
sub valid_page
{
if ($VALID_DOMAIN eq "")
{return;}
$DN=$ENV{'HTTP_REFERER'};
if ($DN eq "")
{return;}
$DN=~tr/A-Z/a-z/;
$VALID_DOMAIN=~tr/A-Z/a-z/;
if ($DN =~ /$VALID_DOMAIN/)
{$stayin=1;}
else {$stayin=0;}
if ($stayin == 0)
{
$errmesg ="<B>Sorry! You can't run this script from your server</B><BR>";
&error_exit;
}
}





parham_m_s
journeyman

Jan 6, 2001, 6:16 PM

Post #2 of 6 (4646 views)
Re: Guestbook Multiple Checkboxes [In reply to] Can't Post

i think it's cuz all of u'r checkboxes have the same name. How would you distinguish one check box from the other. Gotta give em different names, or else one will keep replacing the previous.



Revival Fire
Novice

Jan 6, 2001, 7:47 PM

Post #3 of 6 (4640 views)
Re: Guestbook Multiple Checkboxes [In reply to] Can't Post

Here is a link to a tutorial that states that it is possible to do this....

http://www.stars.com/Authoring/HTML/Forms/check.html



BigRich
Novice

Jan 7, 2001, 1:42 AM

Post #4 of 6 (4634 views)
Re: Guestbook Multiple Checkboxes [In reply to] Can't Post

When you have multiple checkboxes with the same name, they are passed to your script as a list. You are trying to assign a list to a scalar .

If you have 3 checkboxes named check1 check2 and check3, your script sees them as:

check1, check2, check3

When you try to assign the list to a scalar it's the same as,

$field{'service'} = (check1, check2, check3);

$field{'service'} is now check3.

That is why only the last entry is showing in your script.

You'll need to change your script to read in and process the checkboxes as a list. You didn't include the part of your script that processes the form entries so I can't tell you exactly what you need to do but here are a couple of ideas.

if you use CGI.pm assign param('service') to an array variable:

@check_box_values = param('service');

if you use something like:

foreach $entry (@form_input) {
($name, $value) = split)/=/, $entry);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$MYFORM{$name} = $value;
}

just change $MYFORM{$name} = $value;

to

($name eq 'service') ? push(@services, $value) : $MYFORM{$name} = $value;"

Now all of your checkbox values named 'service' are stored in @services.



BigRich






Revival Fire
Novice

Jan 7, 2001, 9:36 AM

Post #5 of 6 (4631 views)
Re: Guestbook Multiple Checkboxes [In reply to] Can't Post

I could not find what needed changing to read these check boxes. I am not very good at PERL, but I can cut and paste into the script if you could tell me what to change. I could not paste the entire CGI script and it was too large to attach, but here is the bottom half....

##################################################################
sub setup_pageentry
{
$tzn=$fields{'signer_email'};
if ($MUNG eq "YES")
{
$tzn =~ s/\./_DoT_/g;
$tzn =~ s/\@/_AT_/g;
}

$PAGE_ENTRY=<<__END_OF_PAGE_ENTRY__;

#######HTML For Guestbook Removed

__END_OF_PAGE_ENTRY__

}
##################################################################
# END USER CONFIGURATION SECTION #
##################################################################

# MAIN ###########################################################
# This is where the script starts execution from
&valid_page;
$the_date=localtime();
&findbook;
&decode_vars;
&test_required;
&valid_address;
&setup_pageentry;
&setup_thankyounote;
&send_thanks;
if (($TELL_ME eq "YES" && $MY_EMAIL ne "") ||
($TELL_ME eq "NO" && $MY_EMAIL ne ""))
{ &notify_me;}
if ($fields{'private'} ne "YES")
{ &write_entry;}
if ( -e $tempmail )
{ unlink($tempmail);}

print "Location: $GUESTBOOK_URL\n\n";
exit;

##################################################################
# NOTE! This routine does a lot more work than it has to so that
# People running Windows 95/98/NT can easily adapt it to use
# a e-mail SMTP program such as BLAT.EXE
##################################################################
sub send_thanks
{
$SBJ = "CEb - Auto-respond ";
$fltime = time;
$flip = $ENV{'REMOTE_ADDR'};
$flip =~s/\.//g;
if ($flip eq "")
{ $flip = "123456";}
$tmpxname=($fltime ^ $flip);
$tempmail="$TEMPDIR/$tmpxname.bbb";
open (OTM,">$tempmail");
print OTM "\n";
print OTM "$THANK_YOU\n\n";
close (OTM);
open(IMZ,"<$tempmail");
@mailtext=<IMZ>;
close(IMZ);

if ($BAD_EMAIL_FORMAT eq "NO" && $SEND_THANKS eq "YES")
{
# IF YOU ARE USING WINDOWS 95/98/NT with BLAT.EXE UNCOMMENT THE LINE BELOW
# system ("$MAIL_PROGRAM $tempmail -t $fields{'signer_email'} -f $MY_EMAIL -s \"$SBJ\" -q");

open (MZ,"|$MAIL_PROGRAM") || die "Content-type: text/html\n\n Unable to send mail";

# IF YOU ARE USING BLAT.EXE, COMMENT THE LINES THAT FOLLOW
# BY PLACING A # SIGN AT THE START OF THE LINE. DO THIS UNTIL
# YOU GET TO THE LINE THAT READS: END OF UNIX PROGRAM CODE

print MZ "To: $fields{'signer_email'}\n";
print MZ "From: $MY_EMAIL\n";
print MZ "Subject: $SBJ\n";

foreach $tomail (@mailtext)
{
print MZ "$tomail";
}
# END OF UNIX PROGRAM CODE
close (MZ);
}
}

##################################################################
# NOTE! Windows 95/98/NT users will have to edit this routine
##################################################################
sub notify_me
{

if ($BAD_EMAIL_FORMAT eq "YES")
{
$SBJ = "CEB Submission - Do not respond";
$tmpename=$MY_EMAIL;
}
else
{
$SBJ = "CEB";
$tmpename=$fields{'signer_email'};
}

# IF YOU ARE USING WINDOWS 95/98/NT with BLAT.EXE UNCOMMENT THE LINE BELOW
# system ("$MAIL_PROGRAM $tempmail -t $MY_EMAIL -f $tmpename -s \"$SBJ\" -q");

open (MZT,"|$MAIL_PROGRAM") || die "Content-type: text/html\n\n Unable to send mail";

# IF YOU ARE USING BLAT.EXE, COMMENT THE LINES THAT FOLLOW
# BY PLACING A # SIGN AT THE START OF THE LINE. DO THIS UNTIL
# YOU GET TO THE LINE THAT READS: END OF UNIX PROGRAM CODE

print MZT "To: $MY_EMAIL\n";
print MZT "From: $tmpename\n";
print MZT "Subject: $SBJ\n";

foreach $tomail (@mailtext)
{
print MZT "$tomail";
}
# END OF UNIX PROGRAM CODE
close (MZT);

}

##################################################################
sub test_required
{
foreach $tst (@mandatory)
{
if ($fields{$tst} eq "")
{
$errmesg ="<B>You did not fill in all of the required information.<BR>";
$errmesg .= "Press your BACK BUTTON to return to the entry form!</B>";
&error_exit;
}
}
}

##################################################################
sub decode_vars
{
$i=0;
if ( $ENV{'REQUEST_METHOD'} eq "GET")
{ $temp=$ENV{'QUERY_STRING'};}
else { read(STDIN,$temp,$ENV{'CONTENT_LENGTH'});}
@pairs=split(/&/,$temp);
foreach $item(@pairs)
{
($key,$content)=split(/=/,$item,2);
$content=~tr/+/ /;
$content=~s/%(..)/pack("c",hex($1))/ge;
#strip comments to prevent server side include calls
$content =~ s/\n";
print WRBK "$PAGE_ENTRY\n";
}
else
{
print WRBK "$line\n";
}
}
close(WRBK);
&drop_the_lock;
}

##################################################################
sub get_the_lock
{
$lockfile="$TEMPDIR/bnbbook.lck";
local ($endtime);
$endtime = 60;
$endtime = time + $endtime;
while (-e $lockfile && time < $endtime)
{
# Do Nothing
}
open(LOCK_FILE, ">$lockfile");
}

##################################################################
sub drop_the_lock
{
close($lockfile);
unlink($lockfile);
}


##################################################################
sub valid_address
{
if ($fields{'signer_email'} eq "")
{
$BAD_EMAIL_FORMAT="YES";
return;
}
$testmail = $fields{'signer_email'};
if ($testmail =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/ ||
$testmail !~ /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/)
{
$BAD_EMAIL_FORMAT="YES";
return;
}
else
{
$BAD_EMAIL_FORMAT="NO";
}
}

##################################################################
sub valid_page
{
if ($VALID_DOMAIN eq "")
{return;}
$DN=$ENV{'HTTP_REFERER'};
if ($DN eq "")
{return;}
$DN=~tr/A-Z/a-z/;
$VALID_DOMAIN=~tr/A-Z/a-z/;
if ($DN =~ /$VALID_DOMAIN/)
{$stayin=1;}
else {$stayin=0;}
if ($stayin == 0)
{
$errmesg ="<B>Sorry! You can't run this script from your server</B><BR>";
&error_exit;
}
}




BigRich
Novice

Jan 7, 2001, 10:49 AM

Post #6 of 6 (4629 views)
Re: Guestbook Multiple Checkboxes [In reply to] Can't Post

The subroutine below is where you need to make the changes but there is no way that that is the complete subroutine in your script.

##################################################################
sub decode_vars
{
$i=0;
if ( $ENV{'REQUEST_METHOD'} eq "GET")
{ $temp=$ENV{'QUERY_STRING'};}
else { read(STDIN,$temp,$ENV{'CONTENT_LENGTH'});}
@pairs=split(/&/,$temp);
foreach $item(@pairs)
{
($key,$content)=split(/=/,$item,2);
$content=~tr/+/ /;
$content=~s/%(..)/pack("c",hex($1))/ge;
#strip comments to prevent server side include calls
$content =~ s/\n";
print WRBK "$PAGE_ENTRY\n";
}
else
{
print WRBK "$line\n";
}
}
close(WRBK);
&drop_the_lock;
}

##################################################################


 
 


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

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