/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Making the listbox ​​have a specific value

Hi Friends… I have a list:
[COLOR=”#B22222″]
<select name=”Codigos” id=”Codigos” >
<option value=”1″>En Transito</option>
<option value=”2″>Afianzando</option>
<option value=”3″>Habitual</option>
<option value=”4″ >Preferente</option>
</select>[/COLOR]

I need the list to be positioned at a certain value that I get from a database, for example, suppose the value obtained is 3, then the list must be positioned at “Habitual”

Can anybody help me?
Thanks

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 22.2013 — Add [B]selected="selected"[/B] to the desired <option> tag.
Copy linkTweet thisAlerts:
@sinchanauthorFeb 22.2013 — Yes... but.. I need to do this automatically. The value list "selected" [U]depends on the value of another variable.[/U]

eJ: var x = 2 -> list must be positioned in "Afianzado" (automatically)

var x = 3 -> list must be positioned in "Habitual"
Copy linkTweet thisAlerts:
@DerokorianFeb 22.2013 — when you're outputting the dropdown just do a check:

[code=php]
$options = array(
1=>'En Transito',
2=>'Afianzando',
3=>'Habitual',
4=>'Preferente',
);

$x = 2;

echo "<select>n";
foreach( $options as $k => $v ) {
echo "<option value='$k'";
if( $x == $k ) {
echo " selected='selected'";
}
echo ">$v</option>n";
}
echo "</select>n";
[/code]
×

Success!

Help @sinchan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...