55 users online (1 members and 54 guests)  


  Results 1 to 2 of 2

Related

  1. Replies: 4
  2. set multiple cookie    Forum: CGI Perl Forum
    Replies: 0
  3. Mouseover with multiple changes    Forum: Javascript Forum
    Replies: 3
  4. Replies: 1
  5. Multiple Rollovers - HELP!!    Forum: Javascript Forum
    Replies: 3
  1. #1
    maude's Avatar
    New User

    Status
    Offline
    Join Date
    Jul 2009
    Posts
    1

    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. #2
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

    Re: Remembering multiple selections

    The problem is that you end up with several hidden fields with the same name (see below)
    Code:
    <input type='hidden' name='hiddencolor' value='$color'>
    <input type='hidden' name='hiddencolor' value='$color'>
    <input type='hidden' name='hiddencolor' value='$color'>
    <input type='hidden' name='hiddencolor' value='$color'>
    <input type='hidden' name='hiddencolor' value='$color'>
    <input type='hidden' name='hiddencolor' value='$color'>
    <input type='hidden' name='hiddencolor' value='$color'>
    Each time you create a new one, it overwrites the previous. To explain it differently, imagine the confusion if you walked into a room filled with young mothers and their children and simply yelled the word 'Mom'. No one would know what to do. HTML would have the same issue if it did not take just the last value to pass on.

    To resolve your issue, there are a couple options available.

    First, create and array of values. You can do this by adding brackets to the end of the name:
    Code:
    <input type='hidden' name='hiddencolor[]' value='$color'>
    <input type='hidden' name='hiddencolor[]' value='$color'>
    <input type='hidden' name='hiddencolor[]' value='$color'>
    <input type='hidden' name='hiddencolor[]' value='$color'>
    <input type='hidden' name='hiddencolor[]' value='$color'>
    <input type='hidden' name='hiddencolor[]' value='$color'>
    <input type='hidden' name='hiddencolor[]' value='$color'>
    Second, you can create a unique value for the field name by tacking a number on to the end. (The syntax may not be completely accurate as I am primarily a PHP programmer, but the principle is the same)
    Code:
    $x=1;
    @color = param('color');
    foreach $color (@color) {
        print "<input type='hidden' name='hiddencolor$x' value='$color'>";
        $x++;
    }



Tags for this Thread