Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Menu

Status
Not open for further replies.

msscott

Programmer
Joined
Sep 12, 2001
Messages
8
Location
US
Hello, all! How do I create a dynamic drop-down menu that displays values which are stored in a separate *.txt file. Is that doable with Perl?
Thanks!
Shane
 
Well, the separate text files bit is certainly doable with Perl. Asking how you create one is a bit of a wide question though. Are you stuck with the general concept of how to go about it, or are you totally new to Perl?
 
Yes, I am new to Perl. And let me explain why I am pushing for the text file concept. The data for the menu be coming from a database (which I am not planning on connecting to directly) and it will changes on a regular basis. The reason I want to reference a text file is so that I can run a weekly report from the database and save the report as the text file, which my menu can then reference. I may just be ignorant, so I am open to other ideas.
Thanks!
Shane
 
Given a text file that looks like,

option 1
option 2
option 3
option 4


You can build a drop down with those options like,
Code:
#!/usr/local/bin/perl

# open your text file
open(TXT,&quot;<dropdown.txt&quot;) or 
    showError(&quot;Failed to open dropdown.txt, $!&quot;);

# push each line onto the array
while (my $line = <TXT>) { push @options, $line; }

# close the text file.
close TXT;

# Normal HTML stuff....
# Build the page up to the options of the 'select'
print &quot;Content-type: text/html\n\n&quot;;
print &quot;<html><head><title>silly demo page</title></head><body>
      <form 
       action='[URL unfurl="true"]http://www.your_server.com/this_cgi.cgi'[/URL] 
       method='post'>
      <select name='dropdown'>&quot;;

# foreach option, print appropriate HTML
foreach (@options) { print &quot;<option>$_</option>\n&quot;; }

# close out the HTML appropriately
print &quot;</select><input type='submit'></form>
       </body></html>&quot;;


# a sub to shoot errors at the browser.  Usually
# cgi errors go to STDOUT which you will never
# see in the browser.  This makes trouble shooting
# a lot easier.
sub showError
{
my $e = shift;
print &quot;<p>$e</p></body></html>&quot;;
exit;
}


HTH If you are new to Tek-Tips, please use descriptive titles, check the FAQs,
and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top