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:
Cant get my checkboxes to display all the users selected ones

 



2huskys
New User

Dec 7, 2010, 8:44 AM

Post #1 of 2 (2083 views)
Cant get my checkboxes to display all the users selected ones Can't Post

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.

Code
#!/strawberry/perl/bin/perl 
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

Code
#!/strawberry/perl/bin/perl 
use CGI ':standard';
use CGI::Carp "fatalsToBrowser";

print header, start_html("your prefrences");

$logfile=">>logfile.txt";
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!");
}
else
{
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


Zhris
Enthusiast

Dec 11, 2010, 11:34 AM

Post #2 of 2 (2059 views)
Re: [2huskys] Cant get my checkboxes to display all the users selected ones [In reply to] Can't Post

Hi,

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:


Code
@prefrence = param('prefrence');


These lines might help you when displaying the array:


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


Chris


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

 
 


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

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