in reply to cgi drop down boxes triggering form field population
my $hostname_ref = $dbh->selectcol_arrayref("SELECT hostname FROM perm +_inventory ORDER BY hostname");
Something's missing here... I can't put my finger on it.
Let's take this one step at a time here. Here is a quick code sniglet that I threw together as "proof of concept" to what we're trying to accomplish:
it very faithfully produced the HTML that I was expecting as such:use CGI; my $array_ref=[ 'eenie','meenie', 'miney', 'mo' ]; my $cgi = new CGI; print $cgi->popup_menu(-name=>'my_popup',-values=>$array_ref);
<select name="my_popup" > <option value="eenie">eenie</option> <option value="meenie">meenie</option> <option value="miney">miney</option> <option value="mo">mo</option> </select>
Now let's look at the other half of the equation. Your database query. You did not state which Perl database interface you were using. But for sake of argument I will use the pattern for DBI/DBD and I'll use a PostgreSQL database for my example.
Well... if we run that code we see something very strange:#!/usr/bin/perl -w use strict; use DBI; use CGI; my $dbh = DBI->connect('DBI:Pg:funkymonkey;host=some.host.com', 'user','password') or die $DBI::errstr; my $array_ref = $dbh->selectall_arrayref(qq( select * from person order by surname ) ) or die $dbh->errstr; my $cgi = new CGI; print $cgi->dropdown_menu(-name=>'myselect', -values=>$array_ref);
<select name="my_popup" > <option value="ARRAY(0x928fb1c)">ARRAY(0x928fb1c)</option> <option value="ARRAY(0x928fb40)">ARRAY(0x928fb40)</option> <option value="ARRAY(0x928fb64)">ARRAY(0x928fb64)</option> <option value="ARRAY(0x928fb88)">ARRAY(0x928fb88)</option> <option value="ARRAY(0x928fbac)">ARRAY(0x928fbac)</option> <option value="ARRAY(0x928fbd0)">ARRAY(0x928fbd0)</option> <option value="ARRAY(0x928fbf4)">ARRAY(0x928fbf4)</option> <option value="ARRAY(0x928fc18)">ARRAY(0x928fc18)</option> <option value="ARRAY(0x928fc3c)">ARRAY(0x928fc3c)</option> <option value="ARRAY(0x928fc60)">ARRAY(0x928fc60)</option> <option value="ARRAY(0x928fc84)">ARRAY(0x928fc84)</option> -- stuff deleted -- </select>
So, what's happening? Here is where Data::Dumper is our friend. If we do a
we see:: use Data::Dumper : rest of our code print Dumper($array_ref);
$VAR1 = [ [ 'Berghold' ], [ 'Bach' ], [ 'Bailey' ], [ 'Battistoni' ], [ 'Battistoni' ], [ 'Becker' ], [ 'Belfer' ], [ 'Bennett' ], --- etc ---
What's happening is each row being returned is an array reference and the entire return variable is an array reference to an array of array references.
So, what you need to do is pull the desired elements out of the return to your query and put them in another array reference.
This ought to do it:
| stuff left out... | handwaving... print $cgi->popup_menu(-name=>'my_popup',-values=>[ map { $_ -> [0] } +@$array_ref ]); | |
|
|---|