Go Back   CodingForums.com > :: Server side development > Perl/ CGI

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-31-2009, 09:05 PM   PM User | #1
maude
New to the CF scene

 
Join Date: Jul 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
maude is an unknown quantity at this point
Remembering multiple selections

Hi there,

I have a very simple problem that I'm struggling with... I am trying to use hidden fields to remember user choices from a select list. I must not be getting something, because my code prints the 2nd to last choice the user made, whereas I want to print ALL choices the user has made:

#!/usr/bin/perl -w

use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);

print header;
print start_html("colors");

print "<form action='colors.pl' method='POST'>";
print "<select name='color'>";
print "<option value='red'> Red";
print "<option value='green'> Green";
print "<option value='blue'> Blue";
print "<option value='gold'> Gold";
print "</select>";

@color = param('color');
foreach $color (@color) {print "<input type='hidden' name='hiddencolor'

value='$color'>";}

print "<input type='submit'>";

print "</form>";

@hiddencolors = param('hiddencolor');

print "@hiddencolors";

print end_html;
maude is offline   Reply With Quote
Old 07-31-2009, 09:43 PM   PM User | #2
KevinADC
Senior Coder

 
Join Date: Mar 2006
Posts: 1,274
Thanks: 2
Thanked 39 Times in 38 Posts
KevinADC is on a distinguished road
try changing the select tag:

print "<select name='color'>";

change to:

print "<select name='color' MULTIPLE>";
KevinADC is offline   Reply With Quote
Old 07-31-2009, 09:48 PM   PM User | #3
FishMonger
Super Moderator


 
Join Date: May 2005
Location: Southern tip of Silicon Valley
Posts: 2,753
Thanks: 2
Thanked 149 Times in 144 Posts
FishMonger will become famous soon enoughFishMonger will become famous soon enough
Quote:
Originally Posted by KevinADC View Post
try changing the select tag:

print "<select name='color'>";

change to:

print "<select name='color' MULTIPLE>";
That does work, but technically, it should be:
Code:
print "<select name='color' multiple='multiple'>";
FishMonger is offline   Reply With Quote
Old 07-31-2009, 09:48 PM   PM User | #4
maude
New to the CF scene

 
Join Date: Jul 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
maude is an unknown quantity at this point
Thanks for the reply. I tried that, and all it seems to do is lengthen the select menu. The outcome is still the same
maude is offline   Reply With Quote
Old 07-31-2009, 09:51 PM   PM User | #5
FishMonger
Super Moderator


 
Join Date: May 2005
Location: Southern tip of Silicon Valley
Posts: 2,753
Thanks: 2
Thanked 149 Times in 144 Posts
FishMonger will become famous soon enoughFishMonger will become famous soon enough
Did you submit it a second time?

Since you're wanting the hidden fields which are not defined until after you submit, they won't be there until you submit more than once.
FishMonger is offline   Reply With Quote
Old 07-31-2009, 09:58 PM   PM User | #6
maude
New to the CF scene

 
Join Date: Jul 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
maude is an unknown quantity at this point
You mean hit the submit button multiple times? Yes.
maude is offline   Reply With Quote
Old 07-31-2009, 10:03 PM   PM User | #7
FishMonger
Super Moderator


 
Join Date: May 2005
Location: Southern tip of Silicon Valley
Posts: 2,753
Thanks: 2
Thanked 149 Times in 144 Posts
FishMonger will become famous soon enoughFishMonger will become famous soon enough
Make your selection and submit

Make another selection and submit

After the 2nd submit, you should see the selections from the 1st submit.
FishMonger is offline   Reply With Quote
Old 07-31-2009, 10:20 PM   PM User | #8
maude
New to the CF scene

 
Join Date: Jul 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
maude is an unknown quantity at this point
Yes, that is what is happening. What I would like to happen is after the first submit, the first choice is displayed. After the second submit, the first and second choice are displayed, and so on...
maude is offline   Reply With Quote
Old 07-31-2009, 10:41 PM   PM User | #9
KevinADC
Senior Coder

 
Join Date: Mar 2006
Posts: 1,274
Thanks: 2
Thanked 39 Times in 38 Posts
KevinADC is on a distinguished road
Quote:
Originally Posted by FishMonger View Post
That does work, but technically, it should be:
Code:
print "<select name='color' multiple='multiple'>";
Fish,

What is your reference for that? As far as I know MULTIPLE is one of those few html attributes that does not have a value, like NOWRAP and maybe a few others.

Multiple
[2|3|3.2|4] [X1|X1.1] [IE1|M2A1|N1|O2.1]
Standards Details: In all HTML 4.x/XHTML DTDs
Required? No
Description:
This attribute indicates that more than one option can be selected at one time to be included in the return value to the form processing script.
Values: NA
KevinADC is offline   Reply With Quote
Old 07-31-2009, 11:09 PM   PM User | #10
FishMonger
Super Moderator


 
Join Date: May 2005
Location: Southern tip of Silicon Valley
Posts: 2,753
Thanks: 2
Thanked 149 Times in 144 Posts
FishMonger will become famous soon enoughFishMonger will become famous soon enough
I'd need to a little searching (possible in the rfc's) for a more official source, but this is one reference.

http://www.w3schools.com/tags/tag_select.asp
FishMonger is offline   Reply With Quote
Old 07-31-2009, 11:18 PM   PM User | #11
KevinADC
Senior Coder

 
Join Date: Mar 2006
Posts: 1,274
Thanks: 2
Thanked 39 Times in 38 Posts
KevinADC is on a distinguished road
Thats interesting, I have never seen that before in all my years of writing HTML code. I always used multiple with no value and it works, still does. Just tried it with the above script and FireFox 3.5 and it works fine.

But anyway, a validator says the html output is full of errors anyway.
KevinADC is offline   Reply With Quote
Old 07-31-2009, 11:21 PM   PM User | #12
FishMonger
Super Moderator


 
Join Date: May 2005
Location: Southern tip of Silicon Valley
Posts: 2,753
Thanks: 2
Thanked 149 Times in 144 Posts
FishMonger will become famous soon enoughFishMonger will become famous soon enough
Did you try it under strict DTD? I haven't myself, but I think strict will say that it needs the value.
FishMonger is offline   Reply With Quote
Old 07-31-2009, 11:29 PM   PM User | #13
KevinADC
Senior Coder

 
Join Date: Mar 2006
Posts: 1,274
Thanks: 2
Thanked 39 Times in 38 Posts
KevinADC is on a distinguished road
Not that this does exactly what the OP wants but it does get rid of all the HTML errors:



Code:
#!usr/bin/perl

use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
@color = param('color');
@hiddencolors = param('hiddencolor');
print header;
print start_html("colors"),
      start_form(),
      scrolling_list(-name=>'color',
                     -values=>['Red','Green','Blue','Gold'],
                     -default=>['Pick A Color'],
                     -size=>5,
                     -multiple=>'true');
foreach $color (@color) {
   print "<input type='hidden' name='hiddencolor' value='$color'>";
}
print submit(),end_form();                       
print "@hiddencolors";
print end_html;
and it does output this:

Code:
<select name="color"  size="5" multiple="multiple">
So thanks for the tip Fish.
KevinADC is offline   Reply With Quote
Reply

Bookmarks

Tags
hidden field, select field

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:22 PM.


Advertisement
Log in to turn off these ads.