CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Using info for form select box

 



jr
Novice

Oct 16, 2000, 1:26 PM

Post #1 of 5 (504 views)
Using info for form select box Can't Post

I have the following code, that goes something like this:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


%yearprice = (
1900 => '4375',
1901 => '4375',
1902 => '4375',
);
</pre><HR></BLOCKQUOTE>

I am trying to get this list to populate a select form drop down box each time the page is loaded (rather than hand-code it in).

I am using something along the lines of:

foreach $key (sort keys %yearprice) {
print "<OPTION VALUE=\"$key\">$yearprice</OPTION>\n";
}

Where the value and label would be the year chosen, ie 1900, 1901, but what I have isn't working...can anyone point me in the right direction?


littleindigo
Deleted

Oct 16, 2000, 7:07 PM

Post #2 of 5 (504 views)
Re: Using info for form select box [In reply to] Can't Post

Hey jr, you're sooooo close Smile...

remember that the variables:

$yearprice
%yearprice and...
@yearprice

are all different variables, by calling $yearprice in your code, instead of accessing an value of a hash element, you're accessing the value of a scalar variable that's probably undefined and is therefore coming out blank.(Should'a used "use strict" !!!... post a response if you don't know what that is)

so if the goal here is to access the values held by each key in the %yearprice hash, and since you're iterating through each key in numeric order in your foreach loop... you could do this...
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


foreach $key (sort keys %yearprice) {
print "<OPTION VALUE=\"$key\">$yearprice{$key}</OPTION>\n";
</pre><HR></BLOCKQUOTE>

Note the difference:

Code you posted:
print "<OPTION VALUE=\"$key\">$yearprice</OPTION>\n";

My suggestion:
print "<OPTION VALUE=\"$key\">$yearprice{$key}</OPTION>\n";


follow? The loop starts... on its first iteration, $key is assigned "1900", so in the loop, when printing $yearprice{$key}, the value stored in $yearprice{'1900'} is printed (4375), which is what you were looking for.

Good luck guy. Post a response if you need any clarification.

[This message has been edited by littleindigo (edited 10-16-2000).]

[This message has been edited by littleindigo (edited 10-16-2000).]


jr
Novice

Oct 17, 2000, 7:20 AM

Post #3 of 5 (504 views)
Re: Using info for form select box [In reply to] Can't Post

See, prior to asking this, I had the exact same setup you described, but it wouldn't spit out the code, when I know the code should work since I have used this in a few other scripts (hence the tweaking I posted).

So now I have it back to its original way, which is also the way you explained, and it spits out none of the keys within the select form. I only get an opening form select tag and a closing form select tag (I know it isn't the HTML).

Any new and improved ideas?


littleindigo
Deleted

Oct 18, 2000, 6:16 AM

Post #4 of 5 (504 views)
Re: Using info for form select box [In reply to] Can't Post

Could you do me a favor and post a bigger chunk of the code? It may be a scoping issue...


jr
Novice

Oct 18, 2000, 6:42 AM

Post #5 of 5 (504 views)
Re: Using info for form select box [In reply to] Can't Post

Okay, but this part of the code is very small:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


%yearprice = (
1900 => '4375',
1901 => '4375',
1902 => '4375',
1903 => '4375',
1904 => '4375',
1905 => '4375',
1906 => '4375',
1907 => '4375',
1908 => '4375',
1909 => '4375',
1910 => '4375',
);

# These years and numbers increase up to
# 2000, with differing prices beginning at 1985

# The only place this is called from is:
print qq~
<td><font face="Arial"><select size="1" name="vehic_year">
<OPTION SELECTED></OPTION>
<option value="1999">1999</option> # Prior line inserted for testing~;
foreach $key (sort keys %yearprice) {
print "<OPTION VALUE=\"$key\">$yearprice{$key}</OPTION>\n";
}
print qq~
</select></font></td>~;
</pre><HR></BLOCKQUOTE>

[This message has been edited by jr (edited 10-18-2000).]

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives