beginr has asked for the wisdom of the Perl Monks concerning the following question:

I have the following code to produce a drop down.

It has some Options with more than one word ie. "Desktop Products"

but when i am reading the form value using

param('technology');

it returns only "Desktop" not "Desktop Products"

Source Code follows

calling the function like this

print "<select name='technology'>"; $tech_selected=param('technology'); &print_option('t',$tech_selected); print "</td></tr>"; sub print_option{ my ($opt,$selected)=@_; my ($arr_val,$where,$ind,$sql); my ($dbh,$sth); if ($opt eq 't'){ # Build Tech Drop down $sql="select distinct(technology) from t_pomap"; }else{ # Build NPO Drop down $sql="select distinct(npo) from t_pomap"; } $dbh = DBI->connect('DBI:Pg:dbname=test', 'db_user', ''); $sth=$dbh->prepare ($sql); $sth->execute(); $ind=0; while(my @options=$sth->fetchrow_array()){ if ($selected eq $options[0]){ print "<option value=$options[0] selected=$options[0]>$options[0] +</option>"; } else { print "<option value=$options[0] >$options[0]</option>"; } }#endwhile $dbh->disconnect(); }#endsub

Edited by davorg: tidied formatting.

Replies are listed 'Best First'.
Re: Creating Multi-word Options
by davorg (Chancellor) on Sep 12, 2006 at 08:29 UTC

    In cases like this, it's always a good idea to look at the HTML you are producing. In this case you'll get something like:

    <option value=Desktop Products>Desktop Products</option>

    And any browser will interpret that as:

    <option value="Desktop" Products>Desktop Products</option>

    So what you really want is:

    <option value="Desktop Products">Desktop Products</option>

    You can only omit quotes around HTML attribute values when they only contain alphanumeric characters. Your attribute contains a space, so it must be quoted. In fact it's a good habit to get into to always quote all HTML attribute values.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Creating Multi-word Options
by Jenda (Abbot) on Sep 12, 2006 at 08:08 UTC
      Plus the selected attribute should have no value

      Depends which version of HTML you are using. In XHTML, it should be selected="selected" for the selected option and missing for all other options.

      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

      2006-09-14 Retitled by planetscape, as per Monastery guidelines


      Original title: 'Re^2: elp me'

Re: Creating Multi-word Options
by jdtoronto (Prior) on Sep 12, 2006 at 14:34 UTC
    I think you should take a look at using CGI, hand crafting HTML like this is far too error prone.
    # use warnings and use strict are assumed... use CGI; my $q = CGI->new; while(my @options=$sth->fetchrow_array()){ print $q->popup_menu( -name => 'whatever', -values => \@options, -default => $selected, ); }
    is much less error prone, and will produce correctly quoted HTML without you having to think about it.

    jdtoronto

    PS: All code is untested, see the module documentation.