Solved

Perl - cgi-lib.pl

Posted on 2000-03-20
6
306 Views
Last Modified: 2010-08-05
Hi,

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";
&ReadParse(*input);
$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?

Thanks,

Andy

0
Comment
Question by:context
6 Comments
 
LVL 10

Expert Comment

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

Expert Comment

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

Author Comment

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

Anyway, thanks.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

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

<TABLE>
<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>
<TD>ORANGES</TD></TR>
<TR> <TD><INPUT TYPE="checkbox" VALUE="lemons" NAME="fruit"></TD><TD>LEMONS</TD><TR>
<TR> <TD><input TYPE="submit" VALUE="run it"></TD><TR>
</FORM>
</TABLE>
***********************************************************
Use CGI.pm
place in array
***********************************************************
#c:\perl\bin\perl

########################################
#This script is called:z_fruitsample.pl
use CGI qw/escape unescape/;
$query = new CGI;
@fruit=$query->param("fruit");
#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;

************************************************************
0
 
LVL 16

Expert Comment

by:maneshr
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);

TO...


@in = $input{'fruit'};

Hope that helps
0
 

Accepted Solution

by:
moonlx earned 100 total points
ID: 2639820
use CGI qw/:standard/;
my $query = new CGI;
print $query->header();
<TABLE>
<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>";
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl Script to append xml file 32 191
how to extract data from a table into a fixed length file 10 54
create a gui in perl 3 71
Perl File::Find alternative 1 51
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now