in reply to Re: Printing Variable Variables
in thread Printing Variable Variables

I'm trying to make a CGI script that will take two form fields and let a user enter in an unlimited amount of data points. For instance they will select a city and distance, then a second city and distance, then a third... etc..

You can see by executing my code what I'm trying to do, I just can't get the values from one submission to another to stay.

Any ideas?
#!/usr/bin/perl -w use CGI::Carp qw(fatalsToBrowser); use strict; use CGI; my $cgi = CGI->new(); my $city1 = $cgi->param("city"); # Get a fresh warning point my $distance1 = $cgi->param("distance"); my @parameterNames = $cgi->param; my $numCity = 0; #Counter for all the Cities passed back to the scrip +t foreach(@parameterNames){ if(/city/) { #Check each paramater to see if is a "city" is present $numCity++; #If it is a city, add it to the counter } } print $cgi->header; my $action = "help.pl"; print $cgi->start_form(-action=>$action); my $quit = '1';#Once this # gets high enough, the loop "quits" if ($numCity){ #If there are any cities, print them as hidden fields while($quit <= $numCity) { # my $hiddenCity = "\$city".$quit; # my $hiddenCityValue = eval $hiddenCity; # my $hiddenDistance = "\$distance".$quit; # my $hiddenDistanceValue = eval $hiddenDistance; my $hiddenCityName = "city".$quit; my $hiddenDistanceName = "distance".$quit; my $hiddenCityValue = $cgi->param("$hiddenCityName"); my $hiddenDistanceValue = $cgi->param("$hiddenDistanceName"); print $cgi->hidden("$hiddenCityName", "$hiddenCityValue"); print $cgi->hidden("$hiddenDistanceName", "$hiddenDistanceValue"); print "$hiddenCityName => $hiddenCityValue<br>"; print "$hiddenDistanceName=> $hiddenDistanceValue<br><hr>"; $quit++; } } print <<EOF; <select name="city" tabindex="1"> <option value="1">Columbus</option> <option value="2">Chicago</option> </select> &nbsp&nbsp&nbspDistance:&nbsp<input type="text" name="circularWarningD +istanceStart" size="6" maxlength="6" /> <br> EOF print $cgi->submit('Go'); print $cgi->end_form;

Replies are listed 'Best First'.
Re^3: Printing Variable Variables
by srdst13 (Pilgrim) on Sep 13, 2005 at 11:07 UTC

    In this particular situation, it looks like you just want to concatenate your cities and distances to those already chosen. You could use a single hidden field for each, something like:

    my @cities,@distances; if ($cgi->param('hiddenCities')) { # there are cities and distances # from before @cities = split(';',$cgi->param('hiddenCities')); @distances = split(';',$cgi->param('hiddenDistances')); } if ($cgi->param('Go')) { # form was submitted push @cities,$cgi->param('city'); push @distances,$cgh->param('circularWarningDistanceStart'); } # put list of cities back on the form print $cgi->hidden('hiddenCities',join(';',@cities); print $cgh->hidden('hiddenDistances',join(';',@distances); # .... # do more form stuff here # like asking for more input, etc.

    The code above is untested, but I hope that gives you some ideas you could use.

    Sean