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:
checkboxes in form

 



clued
stranger

Jul 15, 2001, 1:02 PM

Post #1 of 2 (749 views)
checkboxes in form Can't Post

Hi everyone,

I searched this topic before I posted. I read several replies but can't quite grasp the solution to my problem.

I run the form below using a foreach loop to print listings from a text file to an HTML page.

my $file;
foreach $file (@allfiles) {
$file =~ s/^$config{'qlog'}$form{'category'}\///;
$file =~ s/\.dat$//;
my ($url,$num,$desc,$by,$time) = &read_item_file($form{'category'},$file);
print "<TR><TD><font face=arial size=1>$url";
print "</TD><TD><font face=arial size=1>$num</TD><TD><font face=arial size=1>$desc</TD><TD><font face=arial size=1>$by</TD>";
print "<td><a href = \"javascript:OpenPopup('$url');\"><font face=arial size=1>Preview</a></td>";
print "<td><input type=checkbox name=ADD value=$url|$num|$desc|$by></td></tr>";
print "<input type=hidden name=URL value=$url>";
print "<input type=hidden name=NUM value=$num>";
print "<input type=hidden name=DESC value=$desc>";
print "<input type=hidden name=BY value=$by>";
}

I point this form to a subroutine to print the values to a HTML page.
What I need to do is print the values of ONLY the boxes that are checked to another HTML page. I've tried the solutions provided in the search on this forum and still get incorrect results. I know this is tricky because of the checkbox name being "ADD" in all of the values. Is there a subroutine that will sort and print the checked values to an HTML page?

Thanks in advance

Bob



mhx
Enthusiast / Moderator

Jul 15, 2001, 2:30 PM

Post #2 of 2 (742 views)
Re: checkboxes in form [In reply to] Can't Post

Hi Bob,

if you were using CGI.pm the solution would be quite easy. Unfortunately I don't know if you're using it, and if not, how you get the parameters of your form.
Anyway, I've set up a little script that uses CGI.pm and does -- more or less -- something similar to what you need. I hope you can draw the neccessary information from this script.

Code
#!/bin/perl -w 
use strict;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);

print header, start_html('Checkbox-Demo');

if( param() ) {
print map p( "$_ => ".join ', ', param($_) ), param();
}
else {
print start_form(),
map p(checkbox({label => $_, name => 'ADD', value => $_})),
qw(Dogs Cats Birds Fish Gnus);
print submit('GO!'), end_form();
}

print end_html;

Now, here's some explanation. The script will -- if no parameters were passed -- generate a page with a list of checkboxes: Dogs, Cats, Birds, Fish, and Gnus as well as a submit button. This is the else block in the script. As you will notice, all of the checkboxes are named ADD and differ only by their label and value, which are identical.
If you select some of the checkboxes and hit submit, the script is called again -- this time with parameters -- and will enter the if block. This block will simply print a list of all parameters that were passed to the script. If you call param() without an argument, it will return a list of the names of all parameters that were passed to the script. The map function iterates over this list and prints the values for each name. As you will notice when running the script, the value for the parameter ADD is actually a list of all the values of the checked checkboxes. I used the join function to glue the elements of that list together.
I really hope this will help a little bit, if you have further questions, feel free to ask.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"


 
 


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

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