LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   HELP!! Perl script to display drop down menu (https://www.linuxquestions.org/questions/programming-9/help-perl-script-to-display-drop-down-menu-329219/)

domquem 06-01-2005 11:29 AM

HELP!! Perl script to display drop down menu
 
Perl script to display drop down menu on webpage after reading menu items from a mysql table.

I have a mysql table with a field called Course_name - has like 30 course names in it.
Now i need help as i am very green in perl, with a very simple script to access read the table then fetch the course names which will then need to e dispalyed dynamically in a webpage as Drop Down menu / combo box
.
All the30 course menu items should be displayed in the page after which someone can select any option and have the info about the particular choice they select being displayed.

Thanks for helping, will really appreciate.

david_ross 06-01-2005 12:44 PM

Is this what you are looking for?
Code:

my $sth = $dbh->prepare("SELECT course_name FROM table");
$sth->execute();
print "<select>";
while ($course = $sth->fetchrow_array()){
 print "<option>$course</option>";
}
print "</select>";


rose_bud4201 06-01-2005 12:56 PM

Some good things to look up are 1) how to connect to a mysql table in Perl (use the DBI module, makes things very easy, as david_ross showed), and 2) how to dynamically write HTML content. Basically, what you'll need to do is make your web page a perl script (instead of a .html file), which does a whole lot of "print" statements. You can't embed perl snippets in a web page the same way you can with php.

I've a simple little Hello, World! perl script on my website: http://www.cybermenology.com/cgi-bin/stuff.pl

The code is this, and should give you a good starting point for writing your page:
Code:

#!/usr/bin/perl
print "Content-type: text/html\r\n\r\n";

print "<HTML>";
print "<HEAD><TITLE>Hello, world!</TITLE></HEAD>";
print "<BODY>";
print "Hello, world!";
print "</BODY>";
print "</HTML>";


exit(0);

The first 2 lines are required; leave 'em in there or things won't display correctly. Other than that, just print the HTML page as you want it to appear, and embed the database code where you want to populate your drop-down list. If it's in a form, put the option box inside of a form and assign the form whatever action you want taken; once the page is written, the browser doesn't know whether it is rendering a perl page or an html one, and it will act just like a normal html form would.

Edit: a good tutorial starts here.

domquem 06-03-2005 11:26 AM

Many thanks guys ...this is exactly what i needed.
I will surely start coding in perl right away. :)


All times are GMT -5. The time now is 12:12 PM.