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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dropdown box with current value?

Status
Not open for further replies.

PirateElf

Technical User
Jun 30, 2008
10
I have been working on this for a minute now and am curious if someone can tell me where I am going wrong.

Code:
if ($userDB{$i}->{'FieldType'} == 2) {
     my $curval = $userdata{$username}->{$i};
     $ucode = qq~<select name="$i" size="$userDB{$i}->{'FieldSize'}"><option value="$curval">$curval</option>~ . join('', map { '<option value=' . ($_ =~ /^\[(.+)\]/ ? 'selected>' . HTMLescape($1) : '>' . HTMLescape($_)) . '</option>' } split(/\|/,$userDB{$i}->{'Options'})) . '</select>';
	print FieldsRow ($userDB{$i}->{'DisplayName'} || $i, $ucode);

Basically I am trying to do a dropdown box with perl and I want it to show the current value while still allowing me to add options.

Options are seperated like this: hello | Goodbye | I am confused |
 
Franco, in his post above he indicates he tried "eq":

If I do curval eq option I get nothing and it will only show the drop down box

Maybe a case sensitivity issue? He could try:

Code:
if (lc $curval eq lc $option) {

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Sorry about that but I am just attempting things that might work. I am a novice at best and it's hard going over code that should work but doesn't. As per request I tried this:

Code:
if ( $userDB{$i}->{'FieldType'} == 2 ) {
    my $curval = $userdata{$username}->{$i};
    $ucode = qq[<select name="$i" size="$userDB{$i}->{'FieldSize'}">\n];
    my @options = split(/\|/, $userDB{$i}->{'Options'});
    foreach my $option ( @options ) {
        if (lc $curval eq lc $option) {
            $ucode .= qq[ <option value="$curval" selected>$curval</option>\n];
            next;
        }
        $ucode .= qq[<option value="$option">$option</option>\n];
    }
    $ucode .= qq[</select>];  
    print FieldsRow ($userDB{$i}->{'DisplayName'} || $i, $ucode);
}

But this also does not show the current value. I know it has something to do with the way the options are splitting, but can't for the life of me figure why. No wonder the original code had such elegant poetry of an expression as someone above put it.
 
Why don't you add some print statements and debug the output?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I agree with Travis, and I suggested that a while back in this thread.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top