in reply to Drop-down list Option Value

Okay. I guess I blew my explanation. Here are snippets of code from my HTML document and my CGI.

HTML Code

<p>.....Yada, Yada, Yada... <select name="pet" size="1" onChange="showimage()"> <option selected value="pets.gif">Your Favorite Pet</option> <option value="afghanhd.jpg">Big dogs</option> <option value="austter.jpg">Little dogs</option> <option value="ragdoll2.jpg">Cats</option> <option value="fish1.jpg">Fresh water fish</option> <option value="gerbil.jpg">Gerbils</option> <option value="morganhorse.jpg">Horses</option> <option value="parrot.jpg">Parrots</option> <option value="macaw.jpg">Macaws</option> <option value="toucan.jpg">Toucans</option> <option value="snap.jpg">Turtles</option> <option value="iguana.jpg">Iguanas</option> <option value="sinaloan.jpg">Snakes</option> </select> </p>

CGI Code

#!/usr/bin/perl -w require "cgi-lib.pl"; &ReadParse; #color.cgi #Warn browser that HTML is coming print &PrintHeader; print <<"eohtml"; <html><head><title>Thanks for responding</title></head> <body bgcolor = "#FFFFFF"><H1>Thank You</H1> <p>What a coincidence, $in{'firstname'} $in{'lastname'}!!</p> <p>My favorite color is $in{'color'} <strong>AND</strong> I Love $in{' +pet'}.</p> </body></html> eohtml #End of Program

OUTPUT

Thank You

What a coincidence, Jack Gray!!
My favorite color is yellow AND I Love fish1.jpg.

Desired Output

Thank You

What a coincidence, Jack Gray!!
My favorite color is yellow AND I Love Fresh water fish.

I don't want the drop-down list box in my CGI output, only the description from the HTML option statement.

Again, thanks for all the great responses and I apologize for not fully explaining the assignment.

Replies are listed 'Best First'.
Re: Re: Drop-down list Option Value
by data64 (Chaplain) on Feb 02, 2003 at 17:00 UTC

    In your HTML code you have

    <option value="fish1.jp­g">Fresh water fish</option>

    Only the contents of the value attribute are sent to your CGI script by the browser. So if you want your script to display "Fresh water fish", then you either need to put that into the value attribute or have some sort of a lookup table/file where you can match fish1.jpg to "Fresh water fish"

    On an unrelated note, I am not very sure what cgi-lib.pl is, but you should consider using a standard module for parsing the submitted form data such as CGI. Here's the relavent section of Ovid's web tutorial that talks about why you should use CGi.pm


    Just a tongue-tied, twisted, earth-bound misfit. -- Pink Floyd

      Pink Floyd,
      Just a aheads up regarding cgi-lib. The cgi-lib.pl library has become the de facto standard library for creating Common Gateway Interface (CGI) scripts in the Perl language. You can find information on this at Berkeley. I still need to dig into the difference between the two.

        As TStanley pointed out to me, cgi-lib was designed for Perl4. When Perl5 came out with support for modules, cgi-lib was ported to CGI module.

        So unless you are stuck with Perl4, you should be looking at the CGI module which is now the defacto standard.


        Just a tongue-tied, twisted, earth-bound misfit. -- Pink Floyd

        What's your justification for this statement? The self description on the web page? Look at the date on the bottom of the page:
        $Date: 1998/02/20 05:08:40 $

        If you hang around here for very long, it would be pretty clear that CGI is the de facto library for creating cgi scripts.

        As for your question: The only information that is submitted to your cgi program is the value inside the option tag. So you either have to put the desired text in the value attribute, or you have to map the value attributes to some text within the cgi program. Moreover, you probably don't want to have the image names in the value tags; choose a simple alias to place in the value attributes, and then map that value to the appropriate text and file name within the program.

Re: Re: Drop-down list Option Value
by antirice (Priest) on Feb 02, 2003 at 17:08 UTC
    Easiest way to do that is just use a hash mapping the whatever.gif|jpg to the text values. You use hashes in your program already so I guess you don't need an explanation of how to use them. :)

    Note: untested code follows:

    #!/usr/bin/perl -w require "cgi-lib.pl"; &ReadParse; my %hashOfAnimals = ( "pets.gif" => "Your Favorite Pet", "afghanhd.jpg" => "Big dogs", "austter.jpg" => "Little dogs", "ragdoll2.jpg" => "Cats", "fish1.jpg" => "Fresh water fish", "gerbil.jpg" => "Gerbils", "morganhorse.jpg" => "Horses", "parrot.jpg" => "Parrots", "macaw.jpg" => "Macaws", "toucan.jpg" => "Toucans", "snap.jpg" => "Turtles", "iguana.jpg" => "Iguanas", "sinaloan.jpg" => "Snakes" ); #color.cgi #Warn browser that HTML is coming print &PrintHeader; print <<"eohtml"; <html><head><title>Thanks for responding</title></head> <body bgcolor = "#FFFFFF"><H1>Thank You</H1> <p>What a coincidence, $in{'firstname'} $in{'lastname'}!!</p> <p>My favorite color is $in{'color'} <strong>AND</strong> I Love $hash +OfAnimals{$in{'pet'}}.</p> </body></html> eohtml #End of Program


    Updated: Forgot if key has . in it, key needs to be quoted. Doh!

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

      Antirice,
      Thanks for your help. The code required only one simple change; it requires that the jgp/gif option values be enclosed in quotation marks (as well as the descriptions). With thism change, the code worked perfectly.

      Now I would like to expand this cgi code to display the selected image from the HTML file. This may, in fact, be much more advanced than I am right now. However, it's all a learning experience. Any suggestions in displaying the selected image?

      THX!!!

        Well even I can do this one :)
        print <<"eohtml"; <html><head><title>Thanks for responding</title></head> <body bgcolor = "#FFFFFF"><H1>Thank You</H1> <p>What a coincidence, $in{'firstname'} $in{'lastname'}!!</p> <p>My favorite color is $in{'color'} <strong>AND</strong> I Love $hash +OfAnimals{$in{'pet'}}.</p> <p><center><img src="$in{'pet'}"></center></p> </body></html> eohtml
        Just change the above snippet. You might want to add image attributes and other formatting.