Powered by:
FutureQuest Hosting


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

Web Hosting
Shared       
Reseller     
VPS             
Dedicated 
Price:   $(US)
Space:     (MB)
Transfer:(GB)
Platform:


Advertise
Advertising Opportunities
Rate Card

Sponsor
Go Back   Webmaster Forums > Code Forum > CGI Perl Forum


Reply
 
Thread Tools Rate this Webmaster Discussion
  #1  
Old 07-31-2009, 07:55 PM
maude maude is offline
Junior Member
 
Join Date: Jul 2009
Webmaster Discussions: 1
Rep Power: 2
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;


Reply With Quote
  #2  
Old 08-05-2009, 03:30 PM
jthayne's Avatar
jthayne jthayne is offline
Senior Member
 
Join Date: Aug 2008
Location: Texas
Webmaster Discussions: 492
Rep Power: 15
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.
Check out the following sites: www.inbutnotoftheworld.com and www.mythoughtexactly.com.
Reply With Quote
Reply
Sponsor

Bookmarks

Tags
hidden fields

Thread Tools
Rate This Thread
Rate This Thread:

 

Similar Webmaster Discussions
Thread Webmaster Discussion Starter Forum Replies Last Post
Multiple Categories rab7676 Link Exchange 0 10-03-2007 10:14 AM
Disable/Enable form features based on form selections scanreg Javascript Forum 4 04-18-2007 09:23 AM
set multiple cookie slyen CGI Perl Forum 0 03-22-2007 06: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 08:28 AM


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

Copyright © 1999 - 2009 Advanced HTML For Beginners and AHFB2000. All rights reserved.
Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.