/    Sign up×
Community /Pin to ProfileBookmark

using sticky values with dynamically populated drop list

If I have a statically populated drop list (using values stored in database), how can I use sticky values?

I want the chosen value to remain after a failed validation. I know how to do this when I manually populate the drop list:

[code=php]<select name=”name” class=”style1″>
<option value=””>(choose)
<option value=”Name1″ <?php if (isset($_SESSION[‘name’]) && $_SESSION[‘name’] == ‘Name1’) { echo ‘ selected=”selected” ‘; } ?> >Name1
</select>[/code]

However, how would I do this when querying the db for the list like the following?

[code=php]$queryNameDrop = mysql_query(”
SELECT name
FROM user”) or die(‘Query failed: ‘ . mysql_error());

echo “<p class=’style1′>User:</p>”;
echo “<select name=’dropName’ class=’style1′>”;
echo “<option value=”>”;

while($row = mysql_fetch_array( $queryNameDrop ))
{
$list = $row[‘name’];
echo ” nt<option value=”{$list}”>{$list} “;
}
echo “</select>”;[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscApr 05.2007 — The same way you did it in your first example, with an if statement for the select attribute. You just have to determine what your if statement will consist of.

I think its worth mentioning here as well that the <option> tag isn't being closed in the code you have posted
×

Success!

Help @rbragg 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.21,
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,
)...