LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   PERL: populating a drop down box from the DB (https://www.linuxquestions.org/questions/programming-9/perl-populating-a-drop-down-box-from-the-db-85813/)

vous 08-26-2003 05:39 AM

PERL: populating a drop down box from the DB
 
Hello All,

I'm a newbie in PERL and am trying to get a drop down box populated by a db table. I am at the point where I have retrieved the names from the db, but not yet in the drop down box. The code is here below....can somebody give me a hand?

#!/usr/bonsaitools/bin/perl
#
# Test form

use CGI;
use CGI::Carp qw(fatalsToBrowser);

$cgih = new CGI;
print $cgih->header, "\n";

# Declare local variables
my ($databaseName, $databaseUser, $databasePw, $dbh);

# Set the parameter values for the connection
#!/usr/bonsaitools/bin/perl
#
# Test form

use CGI;
use CGI::Carp qw(fatalsToBrowser);

$cgih = new CGI;
print $cgih->header, "\n";

# Declare local variables
my ($databaseName, $databaseUser, $databasePw, $dbh);

# Set the parameter values for the connection
$databaseName = "DBI:mysql:bugs";
$databaseUser = "user44";
$databasePw = "bugzilla";


use DBI qw(:sql_types);
# Connect to the bugs db
$dbh = DBI->connect($databaseName, $databaseUser,
$databasePw) || die "Connect failed: $DBI::errstr\n";

# Statement to be fired
$stmt = "SELECT realname from profiles;";

$sth = $dbh->prepare("$stmt");
$sth->execute;

while (@row = $sth->fetchrow_array) {($realname) = @row;
print "<TR><TD aligin=left>$realname<TD></TR>\n";}

$sth->finish;
$dbh->disconnect;

sk8guitar 08-26-2003 10:14 AM

if you can get the information from the table, then all you need to do is use an html "select" tag.

i'm assuming this is where you get the information

Code:

while (@row = $sth->fetchrow_array)
{
    ($realname) = @row;
    print "<TR><TD aligin=left>$realname<TD></TR>\n";
}

so just do this

Code:

print "<form action=some_page.cgi method=GET>";
print "<select name=real_names>";
while (@row = $sth->fetchrow_array)
{
    ($realname) = @row;
    print "<option value=some_value>$realname</option>";
}
print "</select>";
print "<input type=submit value=submit></form>";


vous 08-27-2003 02:58 AM

sk8guitar, thanks it works! Just one question though.... When I display it in a browser, the drop down box always gets displayed on top of the page.... I've tried re-arranging the code, but it goes to the top every time.... Would you know why, or how I can change that behavior?

vous 08-27-2003 07:41 AM

Duh...! Iwas so hung up that it was something to do with PERL, I overlooked the simple fact that it was an HTML issue....

Anyways,

it is done!

Tx


All times are GMT -5. The time now is 04:27 AM.