"...Remember being a youngster, mom or dad telling you "close the door"? Well the same applies to HTML ...."

Go Back   Webmaster Forums > Code Forum > CGI Perl Forum

Reply
 
Thread Tools Rate this Webmaster Discussion
Old 07-31-2009, 06:55 PM   #1
maude
New Member
 
Join Date: Jul 2009
Webmaster Discussions: 1
Rep Power: 7
maude is on a distinguished road
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 08-05-2009, 02:30 PM   #2
jthayne
 
jthayne's Avatar
 
Join Date: Aug 2008
Location: Texas
Webmaster Discussions: 501
Rep Power: 21
jthayne is the hardest working person in the biz.jthayne is the hardest working person in the biz.jthayne is the hardest working person in the biz.jthayne is the hardest working person in the biz.
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++;
}
__________________
Be sure to click the reputation icon to give rep to the person who helped you.
For web design/development services, check Silentium Designs.
jthayne is offline   Reply With Quote
Reply

Bookmarks

Tags
hidden fields

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 Off
HTML code is Off


Similar Webmaster Discussions
Thread Webmaster Discussion Starter Forum Replies Last Post
Multiple Categories rab7676 Link Exchange 0 10-03-2007 09:14 AM
Disable/Enable form features based on form selections scanreg Javascript Forum 4 04-18-2007 08:23 AM
set multiple cookie slyen CGI Perl Forum 0 03-22-2007 05:11 PM
Mouseover with multiple changes tgr1227 Javascript Forum 3 11-29-2006 10:56 AM
Making Radiobox selections forward to specific URLs lacey Javascript Forum 1 07-29-2005 07:28 AM


All times are GMT -5. The time now is 09:09 PM.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.