...

View Full Version : Remembering multiple selections



maude
07-31-2009, 10:05 PM
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;

KevinADC
07-31-2009, 10:43 PM
try changing the select tag:

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

change to:

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

FishMonger
07-31-2009, 10:48 PM
try changing the select tag:

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

change to:

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

That does work, but technically, it should be:

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

maude
07-31-2009, 10:48 PM
Thanks for the reply. I tried that, and all it seems to do is lengthen the select menu. The outcome is still the same :(

FishMonger
07-31-2009, 10:51 PM
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.

maude
07-31-2009, 10:58 PM
You mean hit the submit button multiple times? Yes.

FishMonger
07-31-2009, 11:03 PM
Make your selection and submit

Make another selection and submit

After the 2nd submit, you should see the selections from the 1st submit.

maude
07-31-2009, 11:20 PM
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...

KevinADC
07-31-2009, 11:41 PM
That does work, but technically, it should be:

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

FishMonger
08-01-2009, 12:09 AM
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

KevinADC
08-01-2009, 12:18 AM
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.

FishMonger
08-01-2009, 12:21 AM
Did you try it under strict DTD? I haven't myself, but I think strict will say that it needs the value.

KevinADC
08-01-2009, 12:29 AM
Not that this does exactly what the OP wants but it does get rid of all the HTML errors:





#!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:


<select name="color" size="5" multiple="multiple">

So thanks for the tip Fish.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum