/    Sign up×
Community /Pin to ProfileBookmark

Keep chosen option in dropdown <select> menu?

Hi,

Helping a friend with a site and thought of a nifty idea(but can’t implement it heh…nice!).

I have a tiny bit of code in my PHP page:

[code=php]
<select name=”dog” onChange=”MM_jumpMenu(‘parent’,this,0)”>
<option value=”choosedog.php” selected>Dog Type</option>

<?php
$results = mysql_query(“SELECT * FROM dogs”);
$numresults = mysql_numrows($results);
$i=0;
while ($i < $numresults) {
$d_id=mysql_result($results,$i,”dogid”);
$dog=mysql_result($results,$i,”dogtype”);
?>
<option value=”choosedog.php?dog=<? echo $d_id ?>”><? echo $dog ?></option>
<?
++$i;
}
?>
</select>
[/code]

So basically when this is selected it checks my db and outputs the list of dogs based on that type.

However, it does not keep my selection in the dropdown menu. It just goes back to ‘Dog Type’ rather than displaying(say) ‘Border Collie’ if that was the one I selected.

Do I need to use sessions to store the value in there?

Can anyone help with this problem, I’m sure it’s something simple.

Many thanks.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@furnissgJun 26.2008 — you need to have the value available somewhere either if its in the post, or session then you just need to add a statement t the option tag

[code=php]

<option value="choosedog.php?dog=<? echo $d_id ?>" <?php if($d_id == $selected_dog) echo 'selected="selected"';?> ><? echo $dog ? ></option>


[/code]


hope that helps
Copy linkTweet thisAlerts:
@invisionauthorJun 26.2008 — OK.

That's very helpful.

So at the top of my code, I'd have something like $selected_dog = $_POST['dog'];

?
Copy linkTweet thisAlerts:
@furnissgJun 26.2008 — yeah of you could just replace the $selected_dog var with $_POST['dog'] in the option tag, either way will work

[code=php]

<option value="choosedog.php?dog=<? echo $d_id ?>" <?php if($d_id == $_POST['dog']) echo 'selected="selected"';?> ><? echo $dog ?></option>

[/code]
Copy linkTweet thisAlerts:
@invisionauthorJun 26.2008 — Thanks again for the reply and patience with this.

I got it sorted. Again really appreciate your help, delighted with the results ?
Copy linkTweet thisAlerts:
@furnissgJun 26.2008 — no problem
×

Success!

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