Enjoy an ad free experience by logging in. Not a member yet?
Register .
07-31-2009, 09:05 PM
PM User |
#1
New to the CF scene
Join Date: Jul 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
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;
07-31-2009, 09:43 PM
PM User |
#2
Senior Coder
Join Date: Mar 2006
Posts: 1,274
Thanks: 2
Thanked 39 Times in 38 Posts
try changing the select tag:
print "<select name='color'>";
change to:
print "<select name='color' MULTIPLE>";
07-31-2009, 09:48 PM
PM User |
#3
Super Moderator
Join Date: May 2005
Location: Southern tip of Silicon Valley
Posts: 2,753
Thanks: 2
Thanked 149 Times in 144 Posts
Quote:
Originally Posted by
KevinADC
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'>";
07-31-2009, 09:48 PM
PM User |
#4
New to the CF scene
Join Date: Jul 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the reply. I tried that, and all it seems to do is lengthen the select menu. The outcome is still the same
07-31-2009, 09:51 PM
PM User |
#5
Super Moderator
Join Date: May 2005
Location: Southern tip of Silicon Valley
Posts: 2,753
Thanks: 2
Thanked 149 Times in 144 Posts
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.
07-31-2009, 09:58 PM
PM User |
#6
New to the CF scene
Join Date: Jul 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
You mean hit the submit button multiple times? Yes.
07-31-2009, 10:03 PM
PM User |
#7
Super Moderator
Join Date: May 2005
Location: Southern tip of Silicon Valley
Posts: 2,753
Thanks: 2
Thanked 149 Times in 144 Posts
Make your selection and submit
Make another selection and submit
After the 2nd submit, you should see the selections from the 1st submit.
07-31-2009, 10:20 PM
PM User |
#8
New to the CF scene
Join Date: Jul 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
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...
07-31-2009, 10:41 PM
PM User |
#9
Senior Coder
Join Date: Mar 2006
Posts: 1,274
Thanks: 2
Thanked 39 Times in 38 Posts
Quote:
Originally Posted by
FishMonger
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
07-31-2009, 11:09 PM
PM User |
#10
Super Moderator
Join Date: May 2005
Location: Southern tip of Silicon Valley
Posts: 2,753
Thanks: 2
Thanked 149 Times in 144 Posts
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
07-31-2009, 11:18 PM
PM User |
#11
Senior Coder
Join Date: Mar 2006
Posts: 1,274
Thanks: 2
Thanked 39 Times in 38 Posts
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.
07-31-2009, 11:21 PM
PM User |
#12
Super Moderator
Join Date: May 2005
Location: Southern tip of Silicon Valley
Posts: 2,753
Thanks: 2
Thanked 149 Times in 144 Posts
Did you try it under strict DTD? I haven't myself, but I think strict will say that it needs the value.
07-31-2009, 11:29 PM
PM User |
#13
Senior Coder
Join Date: Mar 2006
Posts: 1,274
Thanks: 2
Thanked 39 Times in 38 Posts
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.
Jump To Top of Thread
Thread Tools
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
HTML code is Off
All times are GMT +1. The time now is 12:22 PM .
Advertisement
Log in to turn off these ads.