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 |
 
I thought it was
<option selected value=''>

I don't know if it matters where you have selected but that's how i always did it.

You should use the CGI module :)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[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;
 
your are correct that it would be selected but the problem I am running into is it showing the fields that are available. I am not entirely sure as to why the options themselves will not select.
 
What do you mean they won't select? The drop down list won't open? Post the actual HTML code it prints.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[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;
 
Ok basically I have set so that it shows the options like so:

in html
Code:
<select name="Delicious" size=""><option selected value=""></option><option value=>Yes (Show) </option><option value=> No (Off)</option></select>

The problem is that when you select something it does not save in the field. It has something to do with

Code:
. join('', map { '<option value=' . ($_ =~ /^\[(.+)\]/ ? 'selected>' . HTMLescape($1) : '>' . HTMLescape($_)) . '</option>' } split(/\|/,$userDB{$i}->{'Options'})) . '</select>';

However while this will result in it properly showing the drop down fields, anything selected will not save to the database.

While if you did it something like this:
Code:
  $ucode = qq~<select name="$i" size="$userDB{$i}->{'FieldSize'}"><option selected value="$curval">$curval</option><option value="$userDB{$i}->{'Options'}">$userDB{$i}->{'Options'}</option></selected>

But that is obviously wrong as it doesn't check for the seperator and won't handle multiple values.
 
Well.. it won't save as you don't have any values. They are all blank.

I don't understand why your last example won't work.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[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;
 
the values are stored in
$userDB{$i}->{'Options'}

But the last example won't work due to the fact I can't seem to get the expression to undertand the | seperator
 
I can't modify posts but this works a bit better but it still won't show the currently selected variable

Code:
	$ucode = qq~<select name="$i" size="$userDB{$i}->{'FieldSize'}">~ . join('', map { '<option ' . ($_ =~ /^\[(.+)\]/ ? (HTMLescape($i) eq HTMLescape($1) ? selected : '') . '>' . HTMLescape($1) : (HTMLescape($$i) eq HTMLescape($_) ? selected : '') . '>' . HTMLescape($_)) . '</option>' } split(/\|/, $userDB{$i}->{'Options'})) . '</select>';

The value of the currently selected option would be $curval but $curval only works if I do this:

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

Anyone know how I can make it play nice?
 
i think you should drop the join / map / split combination/poetry and redesign what you want to do with a simple foreach or while loop...as map first of all returns a list takes much more memory and processing time than a simple foreach loop and second because it looks ugly.

is it possible to give us some example values of $userDB{$i}->{'Options'} and some of $i and the final result that you want to achive with example values?

we might be able to be more help this way


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Hey pengo - long time. Nice to have you back...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
I agree with pengo, simplify the code until you get it working, and then go back to split/map/join if you really want to. First thing to do is just loop through the values of $userDB{$i}->{'Options'} and see what they are:

Code:
for (split(/\|/, $userDB{$i}->{'Options'}) {
    print $_,"\n";
}



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
New Poststevexff (Programmer)
1 Jul 08 11:58
Hey pengo - long time. Nice to have you back...

nice to be back mate...long time huh?....I was living at St.Marteen...Caribbean...no time to log in...too much work and too much partying...lol


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
How'd you get that gig :) Fun place to live.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[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;
 
Well basically I am borrowing from the master code that does this for the cms that I use. The problem though is I am new to perl at best and not entirely sure what commands will get my result.

Basically:

I tried to simplify it and got an error but it was just a quick thing:

Code:
	if ($userDB{$i}->{'FieldType'} == 2) {
        	my $curval = $userdata{$username}->{$i};
            my $loop = for (split(/\|/, $userDB{$i}->{'Options'}) {print qq~ <option value="$_">$_</option>~;}
     			$ucode = qq~<select name="$i" size="$userDB{$i}->{'FieldSize'}">~;      
                $ucode .= $loop;
                $ucode .= qq ~</select> ~;
	print FieldsRow ($userDB{$i}->{'DisplayName'} || $i, $ucode);
			}

$userDB{$i}->{'Options'}) is what holds the options for the particular field. So say you had a drop down of Yes, No, Maybe. Each of these is held in Options seperated by a |.
$i in this case is what is stored in that particular fields options.

What I want is simple but I am just unsure of how to do it. I want it to show the drop down box of Yes, No, Maybe that is in Options. And then seperate it nicely and also check to see what the $curval is.

This is not syntax correct but this is what I want when it produces html

Code:
<select name="ItsALie" size="">
<option value"Yes">Yes</option>
<option value"No" selected>No</option>
<option value="maybe">Maybe</option>
</select>

The for loop seems to keep giving me an error.
 
well the philosophy is this...
Code:
if ( $userDB{$i}->{'FieldType'} == 2 ) {
	my $select = qq[
		<select name="$i" size="$userDB{$i}->{'FieldSize'}">
	];
	my @options = split '|', $userDB{$i}->{'Options'};
	foreach my $option ( @options ) {
		$select .= qq[
			<option value="$option">$option</option>
		];
	}
	$select .= qq[</select>];
}


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Thank you I tried working with that but this

Code:
	if ( $userDB{$i}->{'FieldType'} == 2 ) {
	my $curval = $userdata{$username}->{$i};
    $ucode = qq[
        <select name="$i" size="$userDB{$i}->{'FieldSize'}">
    ];
    my @options = split(/\|/, $userDB{$i}->{'Options'});

    foreach my $option ( @options ) { 
    $ucode .= qq[
            <option value="$option">$option</option>
        ];
}
    $ucode .= qq~ <option value="$curval" selected>$curval</option>~;
    $ucode .= qq[</select>];	
	print FieldsRow ($userDB{$i}->{'DisplayName'} || $i, $ucode);
			}


Produces this in html

Code:
        <select name="ItsALie" size="">
    
            <option value="[cake]">[cake]</option>
        
            <option value="[cake]">[cake]</option>
        
            <option value="pie">pie</option>
        
            <option value="Lie">Lie</option>
         <option value="[cake]" selected>[cake]</option></select>

I can't figure out why it shows so many different versions of the selected value and also the [] around a name is supposed to note that is the default value. Not necessarily what was selected.
 
try 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 ($curval eq $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);
}

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I don't get it. If I do curval eq option I get nothing and it will only show the drop down box. However if I do a currant value match it sort of works, but just the same as the above code. I am not entirely sure why it won't match the curval if I use eq or why it shows multiple iterations of curval in this form:

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 ($curval =~ m/(.*)/i) {
            $ucode .= qq[ <option value="$curval" selected>$curval</option>\n];
        }
        $ucode .= qq[<option value="$option">$option</option>\n];
    }
    $ucode .= qq[</select>];  
    print FieldsRow ($userDB{$i}->{'DisplayName'} || $i, $ucode);
}
 
Edit would be nice...

here is another form, but this one only produces a long drop down of curval...

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 ($curval =~ m/(.*)/i) {
            $ucode .= qq( <option value="$curval" selected>$curval</option>);
            next;
        }
        $ucode .= qq(<option value="$option">$option</option>);
    }
    $ucode .= qq[</select>];  
    print FieldsRow ($userDB{$i}->{'DisplayName'} || $i, $ucode);
}
 
The line [tt]if($curval=~m/(.*)/i){[/tt] is nonsense, as the expression is always true and the capturing parentheses are there for nothing. Kevin gave you the correct suggestion above, using [tt]eq[/tt] .
Every time you come back, you are rewriting entirely your code: please stick on a version until it is corrected, stating exactly what is going wrong and displaying the input data and what you are getting.


Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top