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

I'm using CGI.pm to process an HTML form and am unable to properly print the array that results from the checkboxes checked on the HTML form. I get the data to print in a list but the first line returned contains the full array contents along with the first element. Then the remaining elements are printed. Output and code snipet is below.

my @classes = param('class'); foreach my $class (@classes) { print "$class <br>\n"; }

Output (3 checkboxes were selected):

Tai Chi IIYoga for Power5KTai Chi II

Yoga for Power

5K

Output should look like:

Tai Chi II

Yoga for Power

5K

Your help is really appreciated. I am under a deadline and am starting to freak out.

Replies are listed 'Best First'.
Re: Problem printing checkbox response array using CGI.pm
by Anonymous Monk on Aug 19, 2011 at 06:12 UTC

    Your question makes no sense to me, maybe you can simplify it by posting running code that reproduces your problem (as per How do I post a question effectively?), something like

    #!/usr/bin/perl -- use strict; use warnings; use CGI::Pretty; my @fudge = 1..3; my $q = CGI::Pretty->new ( { class => \@fudge } ); print "{$_}\n" for $q->param('class'); print $q->checkbox_group(-name=>'class'), "\n" __END__ {1} {2} {3} <label> <input type="checkbox" name="class" value="1" checked="checke +d" />1 </label> <label> <input type="checkbox" name="class" value="2" checked="checke +d" />2 </label> <label> <input type="checkbox" name="class" value="3" checked="checke +d" />3 </label>
    This what I'm guessing is your issue

      Sorry, I was trying to avoid copying and pasting so much since I have everything on a UNIX server at school. Here is the checkbox HTML. It is a separate file from the cgi file.

      <hr> <b>Pearl Parks and Recreation Class Listings</b> <p> Please use the checkbox(es) to indicate which class/classes you would +like to register for. <p> <table border="1" cellpadding="4"> <tr> <th></th> <th>Title</th> <th>Location</th> <th>Time</th> <th>Start</th> <th>Cost</th> </tr> <tr> <td><input type="checkbox" name="class" value="Pilates"></td> <td>Pilates (13-Adult)</td> <td>Pearlview RECenter</td> <td>9am Sat</td> <td>9/10/2011</td> <td>$100</td> </tr> <tr> <td><input type="checkbox" name="class" value="Zumba"></td> <td>Zumba (13-Adult)</td> <td>Pearlview RECenter</td> <td>10am Sat</td> <td>9/10/2011</td> <td>$100</td> </tr> <tr> <td><input type="checkbox" name="class" value="Tai Chi I"></td> <td>Tai Chi I (13-Adult)</td> <td>Pearlview RECenter</td> <td>12pm Sat</td> <td>9/10/2011</td> <td>$100</td> </tr> <tr> <td><input type="checkbox" name="class" value="Tai Chi II"></td> <td>Tai Chi II (13-Adult)</td> <td>Pearlview RECenter</td> <td>1:30pm Sat</td> <td>9/10/2011</td> <td>$100</td> </tr> <tr> <td><input type="checkbox" name="class" value="Hatha Yoga"></td> <td>Hatha Yoga (13-Adult)</td> <td>Pearlview RECenter</td> <td>11:00am Sun</td> <td>9/11/2011</td> <td>$100</td> </tr> <tr> <td><input type="checkbox" name="class" value="Yoga for Power"></td> + <td>Yoga for Power (13-Adult)</td> <td>Pearlview RECenter</td> <td>9:30am Sun</td> <td>9/11/2011</td> <td>$100</td> </tr> <tr> <td><input type="checkbox" name="class" value=5K Team-Beginner"></td +> <td>5K Team-Beginner (13-Adult)</td> <td>Pearlview RECenter</td> <td>8:00am Tue</td> <td>9/13/2011</td> <td>$100</td> </tr> <tr> <td><input type="checkbox" name="class" value="Cycle Spin Youth & Fa +mily"></td> <td>Cycle Spin Youth & Family (11-Adult)</td> <td>Pearlview RECenter</td> <td>5pm Tue/Thu</td> <td>9/13/2011</td> <td>$100</td> </tr> <tr> <td><input type="checkbox" name="class" value="Low Impact Aerobics"> +</td> <td>Low Impact Aerobics (13-Adult)</td> <td>Pearlview RECenter</td> <td>5pm Wed</td> <td>9/14/2011</td> <td>$100</td> </tr> <tr> <td><input type="checkbox" name="class" value="Weight Training for W +omen"></td> <td>Weight Training for Women (16-Adult)</td> <td>Pearlview RECenter</td> <td>11am Wed</td> <td>9/21/2011</td> <td>$100</td> </tr> </table> <br> <input type="submit" value="Register!"> <input type="reset" value="Clear Form"> </form> </body> </html>

      Here is the full cgi program that I've written so far. Note that this is still a work in progress.

      #!/opt/csw/bin/perl use CGI qw/:standard/; my $name = param('name'); my $address = param('address'); my $city = param('city'); my $state = param('state'); my $zip = param('zip'); my $phone = param('phone'); my $fax = param('fax'); my $email = param('email'); my $email2 = param('email2'); my $gender = param('gender'); my $invoice = param('invoice'); print header(), start_html('Registration Confirmation'), hr(), h2("Pearl County Parks and Recreation"), h4("Class Registration Confirmation"), hr(), p("Thank you for registering with Pearl County Parks and Recreation.") +, p("Please find your class registration confirmation below."), hr(), p("Name:",$name), p("Address:",$address), p("City:",$city), p("State:",$state), p("ZIP:",$zip), p("Phone:",$phone), p("FAX:",$fax), p("Email:",$email), br(), hr(), hr(), p("You are registered for the following class(es)."), p("As requested, your invoice will be sent to you by $invoice."), br(), my @classes = param('class'); foreach my $class (@classes){ print "$class <br>\n"; } end_html();

        The code you posted should never produce the output you described -- like this says, try upgrading CGI

        While I doubt it's your problem, you might want to fix up the following line of HTML:

        <td><input type="checkbox" name="class" value=5K Team-Beginner"></td>

        I think you're missing a " after value=