LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-26-2003, 06:39 AM   #1
vous
Member
 
Registered: Mar 2003
Location: Macondo
Distribution: Mandrake 9.1, 10.1, SuSE 8.1 pro, 10.1, Red Hat 8.0/9.0
Posts: 380

Rep: Reputation: 30
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;
 
Old 08-26-2003, 11:14 AM   #2
sk8guitar
Member
 
Registered: Jul 2003
Location: DC
Distribution: mandrake 9.1
Posts: 415

Rep: Reputation: 30
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>";
 
Old 08-27-2003, 03:58 AM   #3
vous
Member
 
Registered: Mar 2003
Location: Macondo
Distribution: Mandrake 9.1, 10.1, SuSE 8.1 pro, 10.1, Red Hat 8.0/9.0
Posts: 380

Original Poster
Rep: Reputation: 30
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?

Last edited by vous; 08-27-2003 at 04:04 AM.
 
Old 08-27-2003, 08:41 AM   #4
vous
Member
 
Registered: Mar 2003
Location: Macondo
Distribution: Mandrake 9.1, 10.1, SuSE 8.1 pro, 10.1, Red Hat 8.0/9.0
Posts: 380

Original Poster
Rep: Reputation: 30
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Drop connections to port 80 at firewall machine also drop at protected network? Niceman2005 Linux - Security 2 10-27-2005 09:21 AM
HELP!! Perl script to display drop down menu domquem Programming 3 06-03-2005 12:26 PM
iptables - drop all -> allow needed OR allow all -> drop specific lucastic Linux - Security 5 12-21-2004 03:07 AM
populating /dev class_struggle Linux - Newbie 3 07-25-2004 07:39 PM
drop down box / new selection gmarais Programming 4 03-08-2004 03:57 AM


All times are GMT -5. The time now is 11:50 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration