Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #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;

  • #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>";

  • #3
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,944
    Thanks
    2
    Thanked 170 Times in 165 Posts
    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'>";

  • #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

  • #5
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,944
    Thanks
    2
    Thanked 170 Times in 165 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.

  • #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.

  • #7
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,944
    Thanks
    2
    Thanked 170 Times in 165 Posts
    Make your selection and submit

    Make another selection and submit

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

  • #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...

  • #9
    Senior Coder
    Join Date
    Mar 2006
    Posts
    1,274
    Thanks
    2
    Thanked 39 Times in 38 Posts
    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

  • #10
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,944
    Thanks
    2
    Thanked 170 Times in 165 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

  • #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.

  • #12
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,944
    Thanks
    2
    Thanked 170 Times in 165 Posts
    Did you try it under strict DTD? I haven't myself, but I think strict will say that it needs the value.

  • #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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •