Go Premium for a chance to win a PS4. Enter to Win


Perl - cgi-lib.pl

Posted on 2000-03-20
Medium Priority
Last Modified: 2010-08-05

I have a form with a list of checkboxes.
The user checks a number of boxes and clicks submit.
I want to display the values of what's being selected.

I'm using cgi-lib.pl in my script but can't seem to separate the values being submitted.
I'm using the following HTML

<FORM METHOD=POST ACTION=/cgi-bin/myscript.pl>

<INPUT TYPE="checkbox" VALUE="apples" NAME="fruit"></TD><TD>APPLES</TD><TR>
<INPUT TYPE="checkbox" VALUE="oranges" NAME="fruit"></TD><TD>ORANGES</TD><TR>
<INPUT TYPE="checkbox" VALUE="lemons" NAME="fruit"></TD><TD>LEMONS</TD><TR>

I'm using the following PERL:-

require "cgi-lib.pl";
$in = $input{'fruit'};
@in = split(/&/,$in);

If use print $in[0] I get "applesorangeslemons" and get nothing if I use $in[1].

What am I doing wrong?



Question by:context
LVL 10

Expert Comment

ID: 2635829
use cgi.pm. its so easy ..........
LVL 10

Expert Comment

ID: 2635832
yoiu have given all of the check boxes the same name. yoiu shouldnt do that, they cannt be grouped like radio buttons

Author Comment

ID: 2636289
Yep, used cgi.pm and it worked.
Still, thought I could use checkboxes with the same name.

Anyway, thanks.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 2636321
I have used  check boxes with the same name  this way in some of my cgi scripts:

<FORM METHOD=POST ACTION=/cgi-bin/z_fruitsample.pl>
<TR><INPUT TYPE="checkbox" VALUE="apples" NAME="fruit"></TD><TD>APPLES</TD></TR>
<TR> <TD><INPUT TYPE="checkbox" VALUE="oranges" NAME="fruit"></TD>
<TR> <TD><INPUT TYPE="checkbox" VALUE="lemons" NAME="fruit"></TD><TD>LEMONS</TD><TR>
<TR> <TD><input TYPE="submit" VALUE="run it"></TD><TR>
Use CGI.pm
place in array

#This script is called:z_fruitsample.pl
use CGI qw/escape unescape/;
$query = new CGI;
#send MIME and HTML headers
print $query->header("-nph=>1");
print $query->start_html(-title => 'sample');

foreach $i(@fruit){
print "$i<BR>";

print $query->end_html;

LVL 16

Expert Comment

ID: 2636379
"If use print $in[0] I get "applesorangeslemons" and get nothing if I use $in[1]. ..."

whenever you use elements with the same name the return value is sent as an array. the checkbox you have used is a very good example of this.

all that you need to to is change the foll lines.

$in = $input{'fruit'};
@in = split(/&/,$in);


@in = $input{'fruit'};

Hope that helps

Accepted Solution

moonlx earned 300 total points
ID: 2639820
use CGI qw/:standard/;
my $query = new CGI;
print $query->header();
<FORM METHOD=POST ACTION=/cgi-bin/myscript.pl>

<INPUT TYPE="checkbox" VALUE="apples" NAME="apples"></TD><TD>APPLES</TD><TR>
<INPUT TYPE="checkbox" VALUE="oranges" NAME="oranges"></TD><TD>ORANGES</TD><TR>
<INPUT TYPE="checkbox" VALUE="lemons" NAME="lemons"></TD><TD>LEMONS</TD><TR>

#the action (myscript.pl)

use CGI qw/:standard/;
my $query = new CGI;
print $query->header();
print "<HTML><HEAD></HEAD><BODY>";
if($query->param('apples') eq "apples")
    print $query->param('apples')."\n";
if($query->param('oranges')q "oranges")
    print $query->param('oranges');
if($query->param('lemons')q "lemons")
    print $query->param('lemons');

print "</BODY></HTML>";

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question