Home: Perl Programming Help: Beginner:
Cant get my checkboxes to display all the users selected ones

New User

Dec 7, 2010, 8:44 AM

Views: 3825
Cant get my checkboxes to display all the users selected ones

I have to create a newsletter prefrence thing where the user inputs their email and selects the newsletters they wish to receive via checkbox. The next page will output the email and display their prefrences in alphabetical order. I am using an array for the checkboxes but for some reason it only outputs the first one selected in the array rather than all of them. Is there away to use the param function to put the selected results in an array and sort them and then use that to output them. why is only one being displayed rather than them all. below is the code for the first page.

use CGI ':standard';
print header, start_html('Newsletter Prefrences');

@prefrences = ("Comedy", "Current Affairs", "Music", "Documentary", "Soaps" );

print start_form ( { action=>'http://localhost/cgi-bin/assignmentform2.cgi'} );
print "please enter your email ";
print '<INPUT TYPE=Text SIZE=30 NAME=email>';

print p, "please select which newsletters you would like to receive";

for ( $item = 0; $item <= 4; $item++ ) {
print "<INPUT TYPE=\"checkbox\" NAME=\"prefrence\" VALUE=$item>", @prefrences[$item], "\n";


print p, submit, reset;
print end_form, end_html;

Below is the code for the second page

use CGI ':standard';
use CGI::Carp "fatalsToBrowser";

print header, start_html("your prefrences");

open (OUTFILE, $logfile ) || die "Cannot open $logfile: $!";
@infile = <INFILE>;
print $infile[0];
print $infile[2];
close (INFILE);

@prefrences = ("Comedy", "Current Affairs", "Music", "Documentary", "Soaps" );

$selection = param('email');
if ( $selection eq "" ) {
print("You did not input an email please input one!");
print ("Your email is $selection");

$prefrence = param('prefrence');

print p, "You have selected to receive $prefrences[$prefrence]";

print OUTFILE "$selection/$prefrences[$prefrence]\n";
print p, 'Just Logged:', br,"$selection/$prefrences[$prefrence]";
close (OUTFILE);

print end_html;

any help would be great thanks


Dec 11, 2010, 11:34 AM

Views: 3801
Re: [2huskys] Cant get my checkboxes to display all the users selected ones


You are reading an array of values, in scalar context. By doing this with the param method, only the first value is assigned. Try the following instead:

@prefrence = param('prefrence');

These lines might help you when displaying the array:

print "@prefrence"; 
print "$_<br />" foreach (sort {$a cmp $b} @prefrence);


(This post was edited by Zhris on Dec 11, 2010, 11:35 AM)