Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Selecting a value on a pull-down menu

by rodry (Beadle)
on Oct 02, 2000 at 21:55 UTC ( #34938=perlquestion: print w/replies, xml ) Need Help??

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

Perhaps the title of this question is not self-explanatory. Here is what I am trying to do.

I wrote a script that generates a form and populates it with data from a Mysql database. I fill in all the <value> tags with the information I pull from the query.

However, some of the fields have a restrictive set of values that I would like the user to select from a pull-down menu (like a list of countries). I do this by using the <select> tag.

Here comes the problem. When I display the data on the menu, it displays only the first value on the list, not the corresponding value I am populating from the query.

I need to find a way to "print" the <selected> attribute of the <option> tag the correct item. The item list itself comes from a table in the database.

I thought about writing a function that was to be called when it was time to display the pull-down menu, but I realized that I would have to destroy the statement handle before the script finished displaying all the data.

Thanks in advance.

Replies are listed 'Best First'.
Re: Selecting a value on a pull-down menu
by wardk (Deacon) on Oct 02, 2000 at 22:15 UTC
    Not real elegent, but should select the proper drop-down choice
    my @choices = ("Make a selection", "Yes", "No" ); my $UsersChoice = GetUsersChoiceFromDB($user); my $selected = ""; print "<select name=whatever>"; foreach $choice ( @choices ) { if ( $choice eq $UsersChoice ){ $selected = "SELECTED" } else ( $selected = ""; } print "<option $selected value=$choice>$choice"; } print "</select>";
Re: Selecting a value on a pull-down menu
by jreades (Friar) on Oct 02, 2000 at 22:14 UTC

    Without being able to see your table structure it's a little rough to guess at the code, but here's the closest thing that I have (and it uses a list of countries to do it).

    print OUT '<select name="country">'; foreach $row (@$countries) { if ($row->[0] == $billcountry) { print OUT '<OPTION SELECTED VALUE="' . $row->[0] / '">' . $row +->[1]; } else { print OUT '<OPTION VALUE="' . $row->[0] . '">' . $row->[1]; } } print OUT "</select>\n";

    Where the query was:

    $countries = $dbh->selectall_arrayref("SELECT country_id, country_name + FROM country");

    The nice thing about setting up the form this way is that you're getting the unique id values returned in your form submission rather than the name of the country.

    Obviously, in this case $billcountry is a value set elsewhere that you are matching against.

    Hope this helps.

RE: Selecting a value on a pull-down menu
by isotope (Chaplain) on Oct 02, 2000 at 22:00 UTC
    Can you post your code? We might be able to find the problem more easily.

    Update: Ah, ok, that's what I get for trying to think too early on Monday morning. I see what you're trying to do now (too many occurrences of SELECT confused my still-sleeping brain). Looks like plenty of people have shown you the answer, too.

    --isotope
      I would post the code, but the problem is that there isn't any. All the script does is execute a query and then print the HTML with the form and the values for the text boxes. A script I assumed that everyone here is familiar with such code.

      I assume that you wanted to see the code for the function that prints the pull-down menu. I have not even tried that yet.

Re: Selecting a value on a pull-down menu
by jptxs (Curate) on Oct 03, 2000 at 00:59 UTC

    This is the way I do this in a program I have. It handles the case that the first, i.e. blank value is selected when nothing is set beforehand...

    ### Execute the region query $sth->execute() or dienice("585 Couldn't execute statement: " . $sth->errstr); print qq!<TR><TD ALIGN="RIGHT">SC region:<SELECT NAME="reg_name">! +; print qq!<OPTION VALUE=""!; print qq! SELECTED! unless ($REG_NAME); print qq!>ALL REGIONS</OPTION>!; while (my @regions = $sth->fetchrow_array()) { print qq!<OPTION VALUE="$regions[0]"!; print qq! SELECTED! if ($regions[0] eq $REG_NAME); print qq!>$regions[0]</OPTION>\n!; } print qq!</SELECT></TD></TR>!;

    -- I'm a solipsist, and so is everyone else. (think about it)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://34938]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2020-11-26 07:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?