Jump to content
xisto Community
Sign in to follow this  

Editing Drop Down Menu In Php

Recommended Posts

suppose i make a dropdown menu having value 1, 2, 3, 4, 5, one option can be selected, and selected option is stored in database.
now i create an edit page, how do i display the selected value in the menu and other values in dropdown,
for example,
Menu is like
Select One

and i select 3, this 3 is stored in database, now on the edit page i want to show 3 as already selected,
something like that..

[quote]<select name="select_thing">	<option value="1">1</option>	<option value="2">2</option>	<option value="3" selected>3</option>	<option value="4">4</option>	<option value="5">5</option></select>[/quote]
I will be very thankful, if someone could help by explaining how do i make option 3 as selected.. eg. how i know option 3 is selected and stored in db.
Edited by moderator (see edit history)

Share this post

Link to post
Share on other sites

Here are some links to help you out.

This is a wizard that designs a php drop down menu


This is a script that creates a dhtml drop down menu


This site talks about what goes into a php drop down menu and provides files as well


Hopefully that helps give you an idea on what to look for in designing a php drop down menu


"drop down menu in php"

Share this post

Link to post
Share on other sites

Couldn't you just make an if statement? If you know how to get and set the values in the database already, all you'd have to do is somthing like:

<select name="select_thing"><option value="1"<?phpif(mysql_result($result,"","value")==1) echo" selected";?>>1</option><option value="2"<?phpif(mysql_result($result,"","value")==2) echo" selected";?>>2</option><option value="3"<?phpif(mysql_result($result,"","value")==3) echo" selected";?>>3</option><option value="4"<?phpif(mysql_result($result,"","value")==4) echo" selected";?>>4</option><option value="5"<?phpif(mysql_result($result,"","value")==5) echo" selected";?>>5</option></select>

If that's what you meant, here's a more code-conserving way of writing it:
<?php ${mysql_result($result,"","value)}=" selected";?><select name="select_thing"><option value="1"<? echo $1 ?>>1</option><option value="2"<? echo $2 ?>>2</option><option value="3"<? echo $3 ?>>3</option><option value="4"<? echo $4 ?>>4</option><option value="5"<? echo $5 ?>>5</option></select>

Share this post

Link to post
Share on other sites

OK this solution will work also I encourage you to try it it looks more simple to me then others. I cannot tell is it because I use it and I am familiar with it or just because it is simpler then other.

Good luck with whatever you are doing

For the script I think that it doesn't need any more explanation also I would like to point out that I took in count that when you have loaded this value from your database that then you have saved it in the initial_var that is initial variable.

<select name="testlist">  <option <? if(initial_var == "1"){ echo 'selected'; } >?  >1</option>  <option <? else if(initial_var == "2"){ echo 'selected'; } >? >2</option>  <option <? else if(initial_var == "3"){ echo 'selected'; } >? >3</option>  <option <? else if(initial_var == "4"){ echo 'selected'; } >? >4</option>  <option <? else{ echo 'selected'; } >? >5</option>  </select>

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.