Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have an HTML form that has checkboxes that sends to my form. The problem is, it only prints the first value they select rather than each choice. Ie, if they select every possible checkbox from the group, the only value that shows up in the email is Video/DVD. If they select all but the first one, Software it the only one shown.

What am I doing wrong?

The script code is:

my $consider1 = param('consider1'); sub email { open (MAIL, "| $sendmail -t") or die "Cannot open sendmail: $!"; print MAIL "To: $adminmail\n"; print MAIL "From: $usermail\n"; print MAIL "Subject: $subject\n\n"; print MAIL "--------------------\n"; print MAIL "Name: $username\n"; print MAIL "Email: $usermail\n"; print MAIL "Date: $time\n"; print MAIL "--------------------\n"; print MAIL "\n\n"; print MAIL "Question 1:\n"; print MAIL "What is your biggest problem?\n"; print MAIL " $problem1\n"; print MAIL "How would you like this solution delivered?\n"; print MAIL " $delivery1\n"; print MAIL "If Kent created this product, what price would be outs +ide of your budget?\n"; print MAIL " $toomuch1\n"; print MAIL "If Kent created this product, at what price would you +CONSIDER buying it?\n"; print MAIL " $consider1\n"; print MAIL "If Kent created this product, at what price would you +MOST SURELY buy it?\n"; print MAIL " $willbuy1\n"; print MAIL "If Kent created this product, what price would be too +undervalued that you wouldn't purchase it? \n"; print MAIL " $toolittle1\n"; close(MAIL) or die "Cannot close $sendmail: $!"; }
The HTML section is:
<td><p align="left"> <input name="delivery1" type="checkbox" id="delivery1" value=" +Video/DVD"> Video/DVD&nbsp;&nbsp;&nbsp; <input name="delivery1" type="checkbox" id="delivery1" value=" +Online Video"> Online Video &nbsp;<br> <input name="delivery1" type="checkbox" id="delivery1" value=" +Software"> Software &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input name="delivery1" type="checkbox" id="delivery1" value=" +Electronic Book"> Electronic Book <br> <input name="delivery1" type="checkbox" id="delivery1" value=" +Physical Book"> Physical Book <input name="delivery1" type="checkbox" id="delivery1" value=" +Live Seminar"> Live Seminar <br> <input name="delivery1" type="checkbox" id="delivery1" value=" +Telephone Seminar"> Telephone Seminar </p> </td>

Replies are listed 'Best First'.
Re: Checkboxes
by neuroball (Pilgrim) on Jan 17, 2004 at 07:14 UTC

    Last Update: You used two different values in your HTML and perl code, I used the consider1 variable as example. If this is the wrong variable, just replace it with delivery1.

    Your problem is that you defined consider1 as a scalar. I.e. a variable that can hold just one value.

    But, because you have more than one value in your html file, you need an to store the data into an array:

    @consider1 = param('consider1'); ... ... for (@consider1) { print " $_\n"; }

    Always remember: one value = scalar($), list of values = array(@), named values = hash(%).

    /oliver/

    Update: Changed consider one to <code>consider1</code>. Also changed misread variable name from deliver1 to consider1. ... /me needs to go to bad now...

Re: Checkboxes
by flatline (Novice) on Jan 17, 2004 at 17:27 UTC
    I'd hazard a guess that, in addition to neuroball's excellent advise, you may be wanting to get additional data from the form? Just wash, rinse, and repeat for each set of parameters passed:
    @consider = $q->param("consider1"); $problem = $q->param("problem1");
    etc...